How to use curl in a json format - json

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.

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

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'"
}'

Gdrive API : The user does not have sufficient permissions for this file

I try to test Google Drive API from here : https://developers.google.com/drive/api/v3/reference/drives/create
But I get this error : The user does not have sufficient permissions for this file
See also image below :
I don't understand why because I'm logged in with my Google Account, which admin of drive.
here is the code :
curl --request POST \
'https://www.googleapis.com/drive/v3/drives?requestId=1234' \
--header 'Authorization: Bearer [ACCESS_TOKEN]' \
--header 'Accept: application/json' \
--data '{"kind":"drive#drive","name":"TEST_DRIVE"}' \
--compressed
Another test : If I create a file at root level of my drive, works fine. here is the code :
curl --request POST
'https://www.googleapis.com/drive/v3/files'
--header 'Authorization: Bearer [ACCESS_TOKEN]'
--header 'Accept: application/json'
--header 'Content-Type: application/json'
--data '{"kind":"drive#file","name":"TEST"}'
--compressed
And here the result :
200
{
"kind": "drive#file",
"id": "1Itd9QAK_YakAbw9JU4sD8ioa7wBZ8Xud",
"name": "TEST",
"mimeType": "application/octet-stream"
}
other test again : If I want to list my folders at root level of my drive, I get nothing. here is the code :
curl
'https://www.googleapis.com/drive/v3/drives'
--header 'Authorization: Bearer [ACCESS_TOKEN]'
--header 'Accept: application/json'
--compressed
Here is the result :
{
"kind": "drive#driveList",
"drives": []
}
Do you have any idea?
Thanks
L.
Solution found.
files end-point have to be used and not drives to create folder.
code below works :
curl --request POST \
'https://www.googleapis.com/drive/v3/files' \
--header 'Authorization: Bearer [ACCESS_TOKEN]' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{"name":"TEST","mimeType":"application/vnd.google-apps.folder"}' \
--compressed
So what is the difference between drives and folders ?

Curl: Invalid JSON

I'm trying to use the sendgrid curl for adding a new contact.
This is what I got
curl --request PUT \
--url https://api.sendgrid.com/v3/marketing/contacts \
--header 'authorization: Bearer myAPIKEY' \
--data '{"contacts":[{"email":"mymail#icloud.com","first_name":"Karel","last_name":"Deb"}]}’
But it returns this
{
"errors": [
{
"field": "",
"message": "invalid JSON"
}
]
}
What am I doing wrong?
Thank you!
You are not specifying that the body is of type JSON. you need to add --header 'Content-Type: application/json' \ to your request.
curl --request PUT \
--url https://api.sendgrid.com/v3/marketing/contacts \
--header 'authorization: Bearer myAPIKEY' \
--header 'Content-Type: application/json' \
--data '{"contacts":[{"email":"mymail#icloud.com","first_name":"Karel","last_name":"Deb"}]}’

Properly create CURL calls using loop in bash script

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.