Query a REST API with Bearer token via UNIX bash script - bash

I managed to receive a token which looks reasonable and it is saved to the token variable.
I'm using this curl command to query the API:
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header "Authorization: Bearer $token" -d '*JSON format query*' '*Link*' | jq -r
In the API documentation provided by Swagger UI, inputting the JSON format query gives a Response Code 200 with a sensible Response Body. Basically the above code is just copied from the curl command generated by Swagger UI.
But I don't receive any output from my bash script besides:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 655 0 0 100 655 0 3266 --:--:-- --:--:-- --:--:-- 3275
[0][user#ip-xyz:/home/user/abc]
I tried playing around with the script (e.g. replacing $token with something unsensible) and still get the same output which makes me question if the token is even required and/or correct.

Related

Gitlab Schedules API returns only first 25 schedules

I have about 200 pipeline schedules in my gitlab project so i wish to turn em active and inactive whenever i want wtih a single script. I wrote that
for id in $(curl -X GET --header "PRIVATE-TOKEN:<token>" "<project_url>" | jq '.[].id'); do curl --request PUT --header "PRIVATE-TOKEN:<token>" --form active="false" "<project_url>/$id"; done
but the first curl request returns only like first 25 schedules so wish to know how to get all schedules using this API
Thanks in advance
Solved this way
for i in $(eval echo "{1..<page_quantity>}"); do for id in $(curl -X GET --header "PRIVATE-TOKEN:<token>" "<project_url>?per_page=100&page=$i" | jq '.[].id'); do curl --request PUT --header "PRIVATE-TOKEN:<token>" --form active="false" "<project_url>/$id"; done; done

Show progress bar in curl without showing response content

Is there are way within Bash to hide the response body of a curl GET request whilst still showing a progress bar?
--progress-bar Shows a minified version of the default bar however still outputs everything.
Likewise with --silent which hides everything which is unhelpful for the volume of requests.
My current code is,
for i in $(cat scripts/urls.txt); do
file="scripts/output.txt"
content=$(curl --location --request GET $i -H 'Authorization: ...')
"$content" >> $file
cat $file
sleep 5
done
The progress bar is sent to stderr. The response is sent to stdout. To redirect and append the response to a file you could use the >> operator:
while read -r i; do
file="scripts/output.txt"
curl --location --request GET $i -H 'Authorization: ...' >> $file
sleep 5
done < scripts/urls.txt
$ curl -X GET http://www.example.com > /dev/null
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1256 100 1256 0 0 22445 0 --:--:-- --:--:-- --:--:-- 22836

cURL POST request with bearer token results in "Could not resolve host: POST"

I'm issuing the following command in zsh to send a POST request with a bearer token.
curl -o -X POST -H "Authorization: Bearer ${TOKEN}" http://localhost:8090/services/item/0
The output I get is the following:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0curl: (6) Could not resolve host: POST
Update 1
I posted a wrong command which I have corrected now. This is what I'm issuing:
curl -X POST -H "Authorization: Bearer ${TOKEN}" http://localhost:8090/services/item/0
I discovered that the behavior is probably related to running the command in a ZSH. When using Bash the call works fine.
The parameter that follows -o is a file name. Your command line begins with:
curl -o -X POST
... which then means that it will save the output to a file named -X. Then the following word (POST) will be treated as a URL since it doesn't start with a dash...
Using that URL (or host name rather) then causes this error:
Could not resolve host: POST
... because curl fails to resolve that host name. It seems there's no host in your network with that name!
I check must work:
curl -H "Authorization: Bearer ${TOKEN}" http://localhost:8090/services/item/0
Or:
curl -H "Authorization: Bearer ${TOKEN}" http://localhost:8090/services/item/0 -o output.txt

Bash and curl scripting issue [duplicate]

This question already has answers here:
Bash pass variable as argument with quotes
(1 answer)
How to pass quoted arguments from variable to bash script [duplicate]
(1 answer)
Closed 4 years ago.
I am trying to include this curl command (to the Dropbox audit api) in a bash script:
curl -X POST https://api.dropboxapi.com/2/team_log/get_events --header 'Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXX' --header 'Content-Type: application/json' --data '{"time":{"start_time":"2018-07-16T18:07:56Z"}}'
I have replaced the auth token with XXXXX etc.
In my script the timestamp is actually a variable called $LAST.
I build up the curl command like this:
LAST=$(date +"%Y-%m-%dT%H:%m:%SZ")
DROPBOX1=$(echo 'curl -X POST https://api.dropboxapi.com/2/team_log/get_events --header '\''Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXX'\'' --header '\''Content-Type: application/json'\'' --data '\''{"time":{"start_time":"')
DROPBOX2=$(echo '"}}'\''')
CURLSTRING=$DROPBOX1$LAST$DROPBOX2
echo $CURLSTRING
$CURLSTRING
I build it up this way otherwise $LAST was problematic due to all the single and double quotes in the curl POST.
Running that small script gives this output:
curl -X POST https://api.dropboxapi.com/2/team_log/get_events --header 'Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXXX' --header 'Content-Type: application/json' --data '{"time":{"start_time":"2018-07-16T18:07:56Z"}}'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 175 0 127 100 48 232 87 --:--:-- --:--:-- --:--:-- 231
curl: (6) Could not resolve host: Bearer
curl: (6) Could not resolve host: XXXXXXXXXXXXXXXXXXXXXXXXX'
curl: (6) Could not resolve host: application
As can been seen the echo command in the script shows what looks to be a completely valid curl command - indeed if I cut and past that output to the command line, it works. However, as the rest of the output shows in the bash script itself the curl command is not being interpreted properly.
I cannot for the life of me figure out why this is. Clearly echo is preserving the string correctly, whereas the curl command is breaking up the $CURLSTRING somehow. I am obviously not correctly using some combination of single and double quotes, but quite where is breaking is unclear.
thanks
Rob

Run multiple cURL requests in Windows

I am able to execute the below cURL request (generated in PostMan) through a Git Bash in windows, and receive a response as expected.
curl -X GET \ https://test.amazon.com/production/john.stones \
-H 'authorization: Bearer dfgjdrjkdrt' \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-H 'postman-token: 53a3fs9a-5ce3-10fe-7f0a-33e9695ec514' \
-H 'referer: king'
I wish to run multiple cURL requests, around a thousand like the above, with the name being the value that changes in each request. I tried placing them into a batch file but each line is taken as a command, so a bit unsure as to how to structure them. Anyone know how I can run multiple cURL requests, either in sequence or parallel, in Windows 10?
I have addressed this by listing each cURL request in a batch file, replacing all single quotes with double and removed all '\' generated in Postman to divide headers. Only after this does windows run the requests successfully.

Resources