Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

json

bash

curl

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 cUR output?
I went through the curl documentation but could not see any thing/ or I could have missed it.

like image 640
Rishi Avatar asked Jun 17 '14 00:06

Rishi


People also ask

How do I get curl response in JSON format?

To get JSON with Curl, you need to make an HTTP GET request and provide the Accept: application/json request header. The application/json request header is passed to the server with the curl -H command-line option and tells the server that the client is expecting JSON in response.

How do I get my HTTP response code curl?

Show activity on this post. $ (curl --silent http://www.example.org -o >(cat >&1) -w "%{http_code}" 1>&2) 1>/dev/null 200 $ (curl --silent http://www.example.org -o >(cat >&1) -w "%{http_code}" 1>&2) 2>/dev/null <!

How do you do curl request with body?

To post data in the body of a request message using Curl, you need to pass the data to Curl using the -d or --data command line switch. The Content-Type header indicates the data type in the body of the request message.


2 Answers

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") 
like image 168
Remy Lebeau Avatar answered Oct 20 '22 01:10

Remy Lebeau


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" 
like image 34
Enrique Palacio Avatar answered Oct 19 '22 23:10

Enrique Palacio