Properly create CURL calls using loop in bash script - json

I need to call an API where I increment the user ID every time, I have the following in the bash script, but keep getting a Unexpected token ' in JSON at position 2 error. What am I doing wrong?
for ((i=1;i<=5;i++)); do
curl -X POST --header 'Content-Type: application/json' -d "{ 'id': 'person'$i, 'name':
'person', 'info': {} }" 'http://localhost:9999/add'

It is a quoting issue. It is standard for JSON to have double quotes, try this
for ((i=1;i<=5;i++)); do
echo "Adding person"$i
curl -X POST --header 'Content-Type: application/json' --header
'Accept: application/json' --user 'admin' -d '{ "id": "person'$i'", "name":
"person", "info": {} }" 'http://localhost:9999/add'
done

You can use jq, to ​​edit json by shellscript. See this link.

Related

Loop over a curl command throguh a text file using sed

I have a curl commadn that will run through a GitHub runner, the file export.sh looks like:
while read -r auth0 && read -r roles <&3; do
curl --request POST \
--url 'https://YOUR_DOMAIN/api/v2/users/USER_ID/roles' \
--header 'authorization: Bearer MGMT_API_ACCESS_TOKEN' \
--header 'cache-control: no-cache' \
--header 'content-type: application/json' \
--data '{ "roles": [ "ROLE_ID" ] }'
done < final-user-id.txt 3<final-id.txt
I have a file final-user-id.txt, that is like this:
Jack
Amy
Colin
I also have a file final-id.txt:
role_1
role_2
role_3
I want to run export.sh in such a way that every time it picks up a new variable from final-user-id.txt and replace it with auth0 and "ROLE_ID" be replaced with the contents of final-id.txt So the fist output would be:
while read -r auth0 && read -r roles <&3; do
curl --request POST \
--url 'https://YOUR_DOMAIN/api/v2/users/Jack/roles' \
--header 'authorization: Bearer MGMT_API_ACCESS_TOKEN' \
--header 'cache-control: no-cache' \
--header 'content-type: application/json' \
--data '{ "roles": [ "role_1" ] }'
done < final-user-id.txt 3<final-id.txt
So on and so forth depending on the number of variables.
How about something like this:
#!/usr/bin/env bash
while IFS= read -r auth0 && IFS= read -r roles <&3; do
echo curl --request POST \
--url "https://YOUR_DOMAIN/api/v2/users/$auth0/roles" \
--header "authorization: Bearer MGMT_API_ACCESS_TOKEN" \
--header "cache-control: no-cache" \
--header "content-type: application/json" \
--data "{ 'roles': [ '$roles'] }"
done < final-user-id.txt 3<final-id.txt
Output
curl --request POST --url https://YOUR_DOMAIN/api/v2/users/Jack/roles --header authorization: Bearer MGMT_API_ACCESS_TOKEN --header cache-control: no-cache --header content-type: application/json --data { 'roles': [ 'role_1'] }
curl --request POST --url https://YOUR_DOMAIN/api/v2/users/Amy/roles --header authorization: Bearer MGMT_API_ACCESS_TOKEN --header cache-control: no-cache --header content-type: application/json --data { 'roles': [ 'role_2'] }
curl --request POST --url https://YOUR_DOMAIN/api/v2/users/Colin/roles --header authorization: Bearer MGMT_API_ACCESS_TOKEN --header cache-control: no-cache --header content-type: application/json --data { 'roles': [ 'role_3'] }
Although I suggest to use a different fd for final-user-id.txt just i case curl is using/consuming stdin.
Remove the echo if you're satisfied with the output.
Since you're using bash as mentioned by #DennisWilliamson the builtin read command has the -u flag. so the first line could be written as:
while IFS= read -ru3 auth0 && IFS= read -ru4 roles ; do
And the last line should be written like:
done 3<final-user-id.txt 4<final-id.txt

Bitly: is is possible to create customs links via the API

I'd like to create a custom link via the API, that's to say instead of maclede12.co/3R8OeCm maclede12.co/magnifique. It is also referred as custom back-half.
I'm able to patch a link with the API via this request:
curl \
-H 'Authorization: Bearer token' \
-H 'Content-Type: application/json' \
-X PATCH \
-d '{
"id": "maclede12.co/3R8OeCm",
"custom_bitlinks": [
"maclede12.co/magnifique"
],
"tags": [
"bitly",
"api"
]
}' \
https://api-ssl.bitly.com/v4/bitlinks/maclede12.co/3R8OeCm
The tags are updated but I'm not able to get a custom link. The custom_bitlinks parameter comes from the doc but is it still even possible to do it?

How to use variables inside data-raw json string in a curl command

The following script of curl command works fine with variables inside double-quoted string, but how do I use variables (e.g. ip_address and user_id) inside the --data-raw '{...}'?
#!/bin/sh
ip_address="10.0.0.1"
user_id="my-user-id"
websec_token="some_string"
version="v12"
curl -w #curl-format.txt \
--request POST "http://example.com/$version/web" \
--header "Authorization: Bearer $websec_token" \
--header "Content-Type: application/json" \
--data-raw '{
"ipAddress": "10.0.0.1",
"userId": "my-user-id"
}'
Just escape the double quotes:
--data-raw "{
\"ipAddress\": \"$ip_address\",
\"userId\": \"$user_id\"
}"
just add single quote arround the var part is better, as less \ introduced :
--data-raw '{
"ipAddress": "'$ip_address'",
"userId": "'$user_id'"
}'

How to use curl in a json format

i want to to know to where and how to run a json format like this
curl --request POST \
--url https://x.com/xxxx \
--header 'apikey: xxxxxxxxxxxxxxxxxxxxxxxxxx' \
--header 'content-type: application/json' \
--data '{
"filter": {
"whois_name": "xxxxxxxxx"
}'
You can run it from the terminal in a computer which has the cUrl utility installed.

How to post a multi line json string as body in curl inside bash script

I have the following bash script
#!/bin/bash
body=$(cat << EOF
{
"CreatedBy": "$(hostname -f)",
"Id": "$(uuidgen)",
"Type": "TestAlertType",
"AlertCategory": "NonUrgent"
}
EOF
)
curl -H "Content-Type: application/json" -X POST -d $body https://dev.cloudapp.net/v1/
But I get invalid json error on post. What am i missing?
This worked
curl -H "Content-Type: application/json" -X POST -d "$body" https://dev.cloudapp.net/v1/