I've been trying to create a session, even with curl it's giving me something weird (I wiped app id and auth key out in this post):
curl -X POST \
-H "Content-Type: application/json" \
-H "QuickBlox-REST-API-Version: 0.1.0" \
-d '{"application_id": "XXX", "auth_key": "XXXXXXXXXXXXXX", "timestamp": $(date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"), "nonce": "1236221330", "signature": "b51f77e6a233db78a3785e3cf8b27aa4e151bd96"}' \
https://api.quickblox.com/session.json
With this I'm getting back this HTML body:
<body>
<!-- This file lives in public/500.html -->
<div class="dialog">
<h1>We're sorry, but something went wrong.</h1>
<p>We've been notified about this issue and we'll take a look at it shortly.</p>
</div>
</body>
The example is pretty much straight from the doc, except the time stamp part. The signature has Anything I'm doing wrong?
Try this params format
-d "application_id=140&auth_key=7quWEh-k6TqghXe×tamp=1326964049&nonce=414546828&signature=e6e603c251a569e70a2f27a8c71b5017e81e05d5"
not json body
This CURL example works OK
curl -X POST -H "Content-Type: application/json" -H "QuickBlox-REST-API-Version: 0.1.0"
-d '{"application_id":"1","auth_key":"gHT98dDU2zpkKej","nonce":"33432","timestamp":"1375384935","signature":"242f6407b4cd6f0b06d1bca67faac4b57eb21c26"}' http://api.quickblox.com/session.json
just type it in terminal
Related
I am trying to send JSON data containing a mac address to an api using this command:
$value={ "pcModel": "KAT12", "displayType": "DELL U2311H", "graphicsType": "Microsoft Remote Display Adapter", "displayServiceTag": "HV8XP08Q079L", "ipAddress": "172.16.4.194", "recDate": "2022-10-06 16:57:55", "serviceTag": "18LQ9X1;Diskwear:(4.91TBW ; 15393 Hours)", "wolMode": "lanwithpxeboot;CC:101010-0118ZH;os:Ubuntu", "sleepState": "disable", "macAddress": "90:B1:1C:8E:D5:11", "hostName": "CI-KR95-05", "diskMode": "raid", "diskType": "Samsung SSD 850 PRO 512GB;TBW+Hrs:(4.91TB;15393 HrH) ;Clock:3.4GHz;Max Clock:3.67GHz(108%);RAM:32GB" }
curl -X "PUT" -H "Accept: application/json" -H "Authorization: Token 62d85330rf562c4cd6c1fb1a64d1" -d "$value" --url "https://my_api.org/api/inventory/84:2b:2b:a0:0s2:18
but I get the following answer:
user#ubuntu:~$ curl -X "PUT" -H "Accept: application/json" -H "Authorization: Token 62d85df90210cd1a827bc1518c4cd6c1fb1a64d1" -d "$value" --url "https:/my_api/api/inventory/84:2b:2b:a0:0s2:18"
curl: (3) URL using bad/illegal format or missing URL
I tried to escape the ":" colon characters with \ like this
curl -X "PUT" -H "Accept: application/json" -H "Authorization: Token 62d85330rf562c4cd6c1fb1a64d1" -d "$value" --url "https://my_api.org/api/inventory/84\:2b\:2b\:a0\:0s\:18"
but I get no output and it sends nothing.
any Idea how to send this data without having the bad format error?
Thanks a lot
I removed the "?" from $value and added single quotes.
Try this:
#!/bin/bash
value='{ "pcModel": "KAT12", "displayType": "DELL U2311H", "graphicsType": "Microsoft Remote Display Adapter", "displayServiceTag": "HV8XP08Q079L", "ipAddress": "172.16.4.194", "recDate": "2022-10-06 16:57:55", "serviceTag": "18LQ9X1;Diskwear:(4.91TBW ; 15393 Hours)", "wolMode": "lanwithpxeboot;CC:101010-0118ZH;os:Ubuntu", "sleepState": "disable", "macAddress": "90:B1:1C:8E:D5:11", "hostName": "CI-KR95-05", "diskMode": "raid", "diskType": "Samsung SSD 850 PRO 512GB;TBW+Hrs:(4.91TB;15393 HrH) ;Clock:3.4GHz;Max Clock:3.67GHz(108%);RAM:32GB" }'
curl -X "PUT" -H "Accept: application/json" -H "Authorization: Token 62d85330rf562c4cd6c1fb1a64d1" -d "$value" --url "https://my_api.org/api/inventory/84:2b:2b:a0:0s2:18"
Didn't your mother teach you how to format?
value='{
"pcModel": "KAT12",
"displayType": "DELL U2311H",
"graphicsType": "Microsoft Remote Display Adapter",
"displayServiceTag": "HV8XP08Q079L",
"ipAddress": "172.16.4.194",
"recDate": "2022-10-06 16:57:55",
"serviceTag": "18LQ9X1;Diskwear:(4.91TBW ; 15393 Hours)",
"wolMode": "lanwithpxeboot;CC:101010-0118ZH;os:Ubuntu",
"sleepState": "disable",
"macAddress": "90:B1:1C:8E:D5:11",
"hostName": "CI-KR95-05",
"diskMode": "raid",
"diskType": "Samsung SSD 850 PRO 512GB;TBW+Hrs:(4.91TB;15393 HrH) ;Clock:3.4GHz;Max Clock:3.67GHz(108%);RAM:32GB"
}'
$value is not a legal variable name in bash for declaration, only when you want to look it up. use value=
Also, you are missing a " after the URL.
$ curl "http:/cake.com/nom-nom
> "
curl: (3) URL using bad/illegal format or missing URL
The protocol schema is followed by colon-slash-slash ://. so you need one more:
https://
Maybe you want to read the URL rfc:
https://www.ietf.org/rfc/rfc2718.txt
2.1.2 Improper use of "//" following ":"
Contrary to some examples set in past years, the use of double
slashes as the first component of the of a URL
is not simply an artistic indicator that what follows is a URL:
Double slashes are used ONLY when the syntax of the URL's contains a hierarchical structure as described in RFC
2396. In URLs from such schemes, the use of double slashes indicates that what follows is the top hierarchical element for a
naming authority. (See section 3 of RFC 2396 for more details.)
URL schemes which do not contain a conformant hierarchical
structure in their should not use double
slashes following the ":" string.
Thanks for your answers.
I found the reasons why my code was not updating the API
1 - I needed to specify the "Content-Type: "application/json" parameter to tell the API that the data sent hat JSON format otherwise I saw in verbose mode that the data has a "x-www-form-urlencoded" format that the API can't understand.
2 - The data sent with curl must accept a value with the format like this
value='{
"pcModel": "KAT12",
"displayType": "DELL U2311H",
"diskType": "Samsung SSD .."
}'
3 - The curl request must be sent with double quotes around the url and the data ""
So the end request looks like this:
curl -k -X "PUT" -H "Content-Type: application/json"\
-H "Accept: application/json" -H "Authorization: Token 62d85df902101828g84kc4cd6c1fb1a64d1" \
--url "https://my_api.org/api/inventory/84:2b:2b:a0:0s2:18" \
-d "$value"
I am trying to run a bash script, where I would like to make POST calls in a for loop as follows:
for depId in "${depIds[#]}"
do
echo "$depId" <--------------------------------- THIS IS PRINTING PROPER VALUE
curl 'https://student.service.com/api/student' \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-H 'Cookie: UISESSION=abcd' \
--data-raw '{"name":"Student Name","description":"Dummy","depId":$depId}' \ <---- HERE I CANNOT GET THE VALUE OF THE VARIABLE
--compressed
echo "$content"
done
As mentioned above, I cannot get the value of the department id in the URL, with the above form, I am getting a Request Malformed exception. I have even tried with ${depId}, but no luck.
Could anyone please help here ?
Try flipping your quotes around the variable.
--data-raw '{"name":"Student Name","description":"Dummy","depId":'"$depId"'}' \
The first part of the script looks like:
curl -k -X POST -H "Content-Type: application/x-www-form-urlencoded" -H "Cache-Control: no-cache" -d 'username=username&password=password "https://get_token" >> token.txt
token=$(<'token.txt'$'\r')
echo "$token"
The output from echo $token looks fine(417 characters):
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIyTVd0bGo4Y1VPYW8xNmdZTlV4QWlFS3RLcTc1b2JsSEN0c3hKWnlxeExCTkRKb1wvKzNSK1l5XC9YUFhoWExLaytJRVdKSE5CTE9MZmxPNksxemFjYXVlM0t1NnFIQjNRXC9QeUxzUTdVSmVOYk9pS3FDeHhcLzk1dz09IiwibmJmIjoxNDgzOTg5NjgwLCJpc3MiOiJwaHgtcmVtc3QtMDAzIiwiX2NhY2hlSWQiOjE0MzA0OCwiZXhwIjoxNDgzOTkzNDAwLCJpYXQiOjE0ODM5ODk4MDAsImp0aSI6IklER0FBNVYwR1pNU0pBTzk5QlVXTzhOMDY5VlRGTiJ9.H3vD-WfZBFXOII4k-cy7Ey2QM9YXytp31m9-Huj9vKw
Let's see what happens when I try to insert the token into the next part of the script.
echo curl -k -X POST -H "Content-Type: application/json" -H "Authorization: AR-JWT "$token"" -H "Cache-Control: no-cache" -d '{
"values":{
"First_Name": "John",
"Last_Name": "Doe",
}
} ' "https://randomurl.com"
Here is the output:
curl -k -X POST -H Content-Type: application/json -H Authorization: AR-JWT eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIyTVd0bGo4Y1VPYW8xNmdZTlV4QWlFS3RLcTc1b2JsSEN0c3hKWnlxeExCTkRKb1wvKzNSK1l5XC9YUFhoWExLaytJRVdKSE5CTE9MZmxPNksxemFjYXVlM0t1NnFIQjNRXC9QeUxzUTdVSmVOYk9pS3FDeHhcLzk1dz0 -H Cache-Control: no-cache -d { OiJwaHgtcmVtc3QtMDAzIiwiX2NhY2hlSWQiOjE0MzA0OCwiZXhwIjoxNDgzOTkzNDAwLCJpYXQiOjE0ODM5ODk4MDAsImp0aSI6IklER0FBNVYwR1pNU0pBTzk5QlVXTzhOMDY5VlRGTiJ9.H3vD-WfZBFXOII4k-cy7Ey2QM9YXytp31m9-Huj9vKw
"values":{
"First_Name": "John",
"Last_Name": "Doe",
}
} https://randomurl.com
2 Things happen when the token is passed in
"9IiwibmJmIjoxNDgzOTg5NjgwLCJpc3Mi" within the token gets deleted
The 2nd half of the token(After the deleted part) gets inserted into the body
What I've tried:
Using a different token
Use token without passing it into a file first
Moving the Authorization header before the URL
All of these results in the same behavior.
I'm using cygwin64 on Windows 7
When the second cURL command is typed with full token in the correct space into cygwin, it works no problem
Output when set-x is added to the script:
$ ./variable_testing.sh
+ ./variable_testing.sh
+ token=$'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIyTVd0bGo4Y1VPYW8xNmdZTlV4QWlFS3RLcTc1b2JsSEN0c3hKWnlxeExCTkRKb1wvKzNSK1l5XC9YUFhoWExLaytJRVdKSE5CTE9MZmxPNksxemFjYXVlM0t1NnFIQjNRXC9QeUxzUTdVSmVOYk9pS3FDeHhcLzk1dz09IiwibmJmIjoxNDgzOTg5NjgwLCJpc3MiOiJwaHgtcmVtc3QtMDAzIiwiX2NhY2hlSWQiOjE0MzA0OCwiZXhwIjoxNDgzOTkzNDAwLCJpYXQiOjE0ODM5ODk4MDAsImp0aSI6IklER0FBNVYwR1pNU0pBTzk5QlVXTzhOMDY5VlRGTiJ9.H3vD-WfZBFXOII4k-cy7Ey2QM9YXytp31m9-Huj9vKw\r'
+ echo $'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIyTVd0bGo4Y1VPYW8xNmdZTlV4QWlFS3RLcTc1b2JsSEN0c3hKWnlxeEx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WQiOjE0MzA0OCwiZXhwIjoxNDgzOTkzNDAwLCJpYXQiOjE0ODM5ODk4MDAsImp0aSI6IklER0FBNVYwR1pNU0pBTzk5QlVXTzhOMDY5VlRGTiJ9.H3vD-WfZBFXOII4k-cy7Ey2QM9YXytp31m9-Huj9vKw
"values":{
"First_Name": "John",
"Last_Name": "Doe",
}
} ' $'https://randomurl.com\r'
curl -k -X POST -H "Content-Type: application/json" -H "Authorization: AR-JWT eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIyTVd0bGo4Y1VPYW8xNmdZTlV4QWlFS3RLcTc1b2JsSEN0c3hKWnlxeExCTkRKb1wvKzNSK1l5XC9YUFhoWExLaytJRVdKSE5CTE9MZmxPNksxemFjYXVlM0t1NnFIQjNRXC9QeUxzUTdVSmVOYk9pS3FDeHhcLzk1" -H "Cache-Control: no-cache" -d { OiJwaHgtcmVtc3QtMDAzIiwiX2NhY2hlSWQiOjE0MzA0OCwiZXhwIjoxNDgzOTkzNDAwLCJpYXQiOjE0ODM5ODk4MDAsImp0aSI6IklER0FBNVYwR1pNU0pBTzk5QlVXTzhOMDY5VlRGTiJ9.H3vD-WfZBFXOII4k-cy7Ey2QM9YXytp31m9-Huj9vKw
"values":{
"First_Name": "John",
"Last_Name": "Doe",
}
} https://randomurl.com
Full, simplified script that that displays output above
#!/bin/bash
set -x logs
token="eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIyTVd0bGo4Y1VPYW8xNmdZTlV4QWlFS3RLcTc1b2JsSEN0c3hKWnlxeExCTkRKb1wvKzNSK1l5XC9YUFhoWExLaytJRVdKSE5CTE9MZmxPNksxemFjYXVlM0t1NnFIQjNRXC9QeUxzUTdVSmVOYk9pS3FDeHhcLzk1dz09IiwibmJmIjoxNDgzOTg5NjgwLCJpc3MiOiJwaHgtcmVtc3QtMDAzIiwiX2NhY2hlSWQiOjE0MzA0OCwiZXhwIjoxNDgzOTkzNDAwLCJpYXQiOjE0ODM5ODk4MDAsImp0aSI6IklER0FBNVYwR1pNU0pBTzk5QlVXTzhOMDY5VlRGTiJ9.H3vD-WfZBFXOII4k-cy7Ey2QM9YXytp31m9-Huj9vKw"
echo "$token"
echo curl -k -X POST -H \"Content-Type: application/json\" -H \"Authorization: AR-JWT $token\" -H \"Cache-Control: no-cache\" -d '{
"values":{
"First_Name": "John",
"Last_Name": "Doe",
}
} ' "https://randomurl.com"
First: Your token currently has literal CRs in it -- the apparent butchering is caused by those CRs being printed in your echo output, moving the cursor back to the beginning of the line so parts of your token get overwritten with later content. To eliminate them, you can either convert the file into UNIX text format (and ensure that there aren't CRs in any of the commands generating that file), or you can use:
token=$(<token.txt) # read token.txt into "token" variable
token=${token//$'\r'/} # strip CRs from token variable
If you want to pass the Authorization header to curl as a single word, don't end and reinitialize your quotes; keep that single argument inside one set of syntactic quotes.
-H "Authorization: AR-JWT $token"
Finally, make sure your script itself is in UNIX format -- that your editor isn't saving it with DOS newlines.
I have a curl request in below format
curl -v -H "Content-Type:application/json" -H "x-user-id:xxx" -H "x-api-key:yyy" --data '{"logs":"'"${TEST_OUTPUT}"'","pass":"true | false"}' https://razeedash.one.qqq.cloud.com/api/v1/clusters/zzz/api/test_results
This works fine while I do from my MAC terminal. But the same command throws
13:49:26 {
13:49:26 "status": "error",
13:49:26 "message": "Invalid credentials"
13:49:26 }
I saw this post but not sure how else would I send a json body without curly braces. I know that we can save it as a file.json and use the file as body.But for some reasons that cannot be implemented in my scenario
In general, you should avoid trying to build JSON using string interpolation. Use a tool like jq to handle any necessary quoting.
jq -n --argson o "$TEST_OUTPUT" '{logs: $o, pass: "true | false"}' |
curl -v -H "Content-Type:application/json" \
-H "x-user-id:xxx" \
-H "x-api-key:yyy" \
--data #- \
https://razeedash.one.qqq.cloud.com/api/v1/clusters/zzz/api/test_results
However, if you can manage to correctly generate your JSON as you are now, you can just replace the jq command with echo:
echo '{"logs": ...' | curl ...
The #- argument to --data says to read from standard input.
I know how to store a string as a variable, for example: API="http://localhost:4741"
However, for the sake of a CURL request I would like to be able to store on object as a variable that I can access values on, something like OBJ="{name : Joe}". Is this possible?
Right now my CURL request looks like this:
curl --include --request POST localhost:3000/scrape \
--header "Content-Type: application/json" \
--data '{
"url": "http://www.oddsshark.com/stats/gamelog/basketball/nba/20736",
"team": "LA Clippers"
}'
I would like to be able to do something like this, using a dictionary or an object:
TEAM=( ["Clippers"]="http://www.oddsshark.com/stats/gamelog/basketball/nba/20736" )
curl --include --request POST localhost:3000/scrape \
--header "Content-Type: application/json" \
--data '{
"url": "http://www.oddsshark.com/stats/gamelog/basketball/nba/20736",
"team": "${TEAM[Clippers]}"
}'