Unable to simulate curl in postman - json

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.

Related

base64 conversion in context with curl

I would like to send a message and attachment via signal-cli.
I successfully set up docker a container by bbernhard/signal-cli-rest-api.
Normal message sending with curl-statement works fine and statement looks like:
curl -X POST -H "Content-Type: application/json" -d '{\"message\": \"Hello World!\", \"number\": \"+490000000\", \"recipients\": [\"+4900000000"]}' 'http://localhost:48080/v2/send'
The message will be sent to one recipient or many. Also working for a group by groupID.
Question: How to add an attachment like a JPG?
If I add \"base64_attachments\": [\"${ENCODED_IMAGE}")\"] to the statement, then i get the error message {"error":"Couldn't process request - invalid request"}
Full bash script looks like :
#!/bin/bash
INPUT_FILE="/path/to/file/IMG_5098.JPG"
TMPFILE=$(mktemp)
base64 "${INPUT_FILE}" --wrap=0 > "${TMPFILE}"
ENCODED_IMAGE=$(cat "${TMPFILE}")
curl -X POST -H "Content-Type: application/json" -d '{\"message\": \"Hello World!\", \"base64_attachments\": [\"${ENCODED_IMAGE}")\"], \"number\": \"+4900000\", \"recipients\": [\"+4900000000\"]}' 'http://localhost:48080/v2/send'
rm "${TMPFILE}"
I expected the image send as well as the message

How to extract bearer token from curl json response and pass it as authorization header a different api call?

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.

Follow links in cURL responses using a loop

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

Sending POST Request from bash script

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.

How to get cURL to output only HTTP response body (JSON) and no other headers etc

I am using curl in a bash script to fetch the response of a service as below,
response=$(curl -isb -H "Accept: application/json" "http://host:8080/some/resource")
Service response is of json type and on browser I could perfectly fine response.
However curl response has other unwanted things (such as set-cookie, content-length header in this case) and sometimes the actual response is eaten up.
Here is the output of echo $response >
Set-Cookie: rack.session=BAh7CEkiD3Nlc3Npb25faWQGOgZFVEkiRWJlY2JiOTE2M2Q1ZWI4NThjMDdi%0AYjRiOWRjMGMxMGEwYTBkMjE3NmJhZDVjYzY4YjY4ZTlmMTE2ZGVkYWE3MTMG%0AOwBGS
SIJY3NyZgY7AEZJIiVhZmQ2MmUyZGMxMzFmOGEwMjg3NDlhNWM3YmVm%0AN2FjNwY7AEZJIg10cmFja2luZwY7AEZ7B0kiFEhUVFBfVVNFUl9BR0VOVAY7%0AAFRJIi00MTc0OGM2MWNkMzljZTYxNzY3ZjU0
Y2I5OTdiYWRkN2MyNTBkYmU4%0ABjsARkkiGUhUVFBfQUNDRVBUX0xBTkdVQUdFBjsAVEkiLWRhMzlhM2VlNWU2%0AYjRiMGQzMjU1YmZlZjk1NjAxODkwYWZkODA3MDkGOwBG%0A--ee97a62095e7d42129
tontent-Length: 354c8; path=/; HttpOnly
This is breaking my response parsing logic.
I have seen this happening intermittently which is weird.
Is there a way to get "only" json response from cURL output?
I went through the curl documentation but could not see any thing/ or I could have missed it.
You are specifying the -i option:
-i, --include
(HTTP) Include the HTTP-header in the output. The HTTP-header includes things like server-name, date of the document, HTTP-version and more...
Simply remove that option from your command line:
response=$(curl -sb -H "Accept: application/json" "http://host:8080/some/resource")
I was executing a get request an also want to see just the response and nothing else, seems like magic is done with -silent,-s option.
From the curl man page:
-s, --silent
Silent or quiet mode. Don't show progress meter or error messages. Makes Curl mute. It will still output the data you ask for, potentially even to the terminal/stdout unless you redirect it.
Below the examples:
curl -s "http://host:8080/some/resource"
curl --silent "http://host:8080/some/resource"
Using custom headers
curl -s -H "Accept: application/json" "http://host:8080/some/resource"
Using POST method with a header
curl -s -X POST -H "Content-Type: application/json" "http://host:8080/some/resource" -d '{ "myBean": {"property": "value"}}'
You can also customize the output for specific values with -w, below the options I use to get just response codes of the curl:
curl -s -o /dev/null -w "%{http_code}" "http://host:8080/some/resource"