curl -H "Accept: application/json" 'http://example.com'
the response I get is
{"next":"/gibberish"}
So I perform another request:
curl -H "Accept: application/json" 'http://example.com/gibberish'
the response I get is
{"next":"/anotherGibberish"}
I want to be able to do the curl request, get the next information and loop in to the next request.
Do you guys have any idea how to perform this in bash?
EDIT: Any help using jq would also be great.
The last response is a JSON response with an authentication failure - that's where I should stop the loop.
{"unauthorized":"Authenticate with username and password"}
Assuming the next key is either nonexistent in the response or its value is a string forming a valid URL when prepended to http://example.com:
#!/bin/bash -
next=/
while resp=$(curl -sS "http://example.com$next"); do
if ! next=$(jq -er '.next' <<< "$resp"); then
# `resp' is the final response here
# , do whatever you want with it
break
fi
done
Related
I am making a call to OAuth API using curl command by passing username,password and getting the bearer token response as JSON which is in the below format.
curl -X POST https://api.mysite.com/oauth/token -u "login:password"
Response
{
"token_type:"Bearer",
"access_token:" "cfdadfa3234sfsdfxx......",
"issued_at":15234234234,
"expires_in":953343434,
"scope": "asdfasd234234234asfasdfasdfaflalsdfkasjfa;sdfassdflj"
}
I need to get only the access_token value which is the bearer token from this curl JSON response and I need to pass as Authorization header to a different apigee gateway hosted api call.
curl -X GET https://apigee.mysite.com/getorderstatus -H "Authroization Bearer ???need to pass bearer token here ???"
How do I parse the JSON and get the bearer token as variable and pass it to the next API call?
I need to do this on the windows server. My environment is only limited to Windows. I can't install packages like jq due to security reason.
The recommended way to extract something from JSON is jq.
However, if you're really sure that the output is always like this, you might
at=$(curl -X POST https://api.mysite.com/oauth/token -u "login:password" | sed -n 's/[ ,"]//g;s/"access_token://p')
curl -X GET https://apigee.mysite.com/getorderstatus -H "$at"
A bash-only solution would be:
curl -X POST https://api.mysite.com/oauth/token -u "login:password" |
while read line ; do
if [[ "$line" == *access_token* ]] ; then
at=${line%\"*}
at=${at##*\"}
curl -X GET https://apigee.mysite.com/getorderstatus -H "$at"
fi
done
Note that, due to the subshell, at is not available outside the loop.
I am trying to apply a long JSON via curl POST, but fails probably due to syntax. The script also contains multiple arrays in order to fulfil the script variables.
When I run the script in order to print (echo) the JSON, it prints successfully the JSON, which is also validated.
When I run the script in order to apply the JSON (curl -X POST), it fails.
Is there any other option in order to apply the JSON without modifying it?
thank you.
You can add -H "Content-Type: application/json" header value to Post the JSON data to curl command line.
For example :
curl -X POST -H "Content-Type: application/json" -d '{"username":"abc","password":"abc"}' https://api.xyz.com/v2/login
I want to execute a bash script after i make a POST request.So far i am using Postman for sending the request , but i was wondering if i can somehow do it from a bash script as well with a json file as parameter.
I have looked into curl so far but it does not work:
bash file
curl -X POST -d req.json http://localhost:9500
Json file (req.json)
{
"id":5,
"name":"Dan",
"age":33,
"cnp":33,
"children":100,
"isMarried":0
}
I just get the error :
HTTP/1.0 503 Service Unavailable
with the trailing HTML
curl should do the job. This will send a normal POST request using the data in req.json as the body:
curl -X POST -H "Content-Type: application/json" -d #req.json http://localhost:9500
The elements you were missing are -H "Content-Type: application/json" and the # in the data flag. Without the -H flag as above curl will send a content type of application/x-www-form-urlencoded, which most applications won't accept if they expect JSON. The # in the -d flag informs curl that you are passing a file name; otherwise it uses the text itself (i.e. "req.json") as the data.
I have a curl request as below:
curl -i -H "Content-Type: application/json" -X PUT -d '[]' http://localhost:3000/v1/api/current.json
When I hit the above request in command prompt I get status 200 which is ok. I'm trying to simulate this request using Postman but not sure how to handle it, specifically -d '[]'.
-d should be data param which I expect a key-value pair but from above I don't see any key involved?
-d means it is the request body data
Choose the PUT method, then in the Body tab, choose raw with application/json
And fill the [] into the text editor area.
In a bash script i want to get date dynamically and send it along with the
curl call.
i have got the date from the user in the bash script
and in the script am making the below curl call. am already passing the request params using a separate file as below. How do i pass the date?
i have tried like $date, but it is not working, even tried "'$date'".
The below is my curl call:
curl -O -X POST -H "Content-Type: application/json" -d#formparams.json --url http://test.com
Contents of form params json: it has more than 10 params for simplicity iam including only two
{"params":"{"HOSTS:":"1",date=$date}}
in the above i have added date.
But the date is not replaced.
Any help is appreciated.
Use a here document instead of a separate file for the parameters. Inside the here document, you can run the date command in a command substitution to provide the correct date when the document is read.
curl -O -X POST -H "Content-Type: application/json" -d#- --url http://test.com <<EOF
{"params":"{"HOSTS:":"1", "date": "$(date)"}}
EOF
use this json for pass dynamic parameter
{"params":"{"HOSTS:":"1",date="'$date'"}}