如何使curl只输出http响应体(json),而不输出其他报头等等

时间:2021-01-17 16:00:59

I am using curl in a bash script to fetch the response of a service as below,

我在bash脚本中使用curl来获取如下服务的响应,

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.

服务响应是json类型的,在浏览器上我可以很好的响应。然而,curl响应还有其他不需要的东西(比如设置cookie,在本例中是内容长度头),有时实际响应会被耗尽。

Here is the output of echo $response >

这是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.
Appreciate any help! Thx

是否有一种方法可以从curl输出得到“仅”的json响应?我浏览了curl文档,但是什么也看不见/或者我可能会错过它。感谢任何帮助!谢谢

1 个解决方案

#1


72  

You are specifying the -i option:

您正在指定-i选项:

-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...

(HTTP)在输出中包含HTTP-header。HTTP-header包含服务器名称、文档日期、HTTP-version等内容。

Simply remove that option from your command line:

只需从命令行删除该选项:

response=$(curl -sb -H "Accept: application/json" "http://host:8080/some/resource")

#1


72  

You are specifying the -i option:

您正在指定-i选项:

-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...

(HTTP)在输出中包含HTTP-header。HTTP-header包含服务器名称、文档日期、HTTP-version等内容。

Simply remove that option from your command line:

只需从命令行删除该选项:

response=$(curl -sb -H "Accept: application/json" "http://host:8080/some/resource")