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

以下是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.

推荐答案

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

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

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

Json相关问答推荐

PostgreSQL 12.17从JSON数组提取元素

如何编写MongoDB查询以返回数组数组

盒子图显示不正确

JQ-JSON将键转换为对象

当由.sh脚本执行时,AWS查询字符串不会提取任何数据

如何对未知/变量键的字典进行编码?

PowerShell - 将 json 添加到文件内容

Jolt 不打印任何东西

如何在 Apache NiFi 中使用 JoltTransformJson 删除流文件或具有空字段的整个对象?

使用 Groovy 将 XML 转换为 JSON

用于遮蔽卡的 Jolt 规格

JQ 中的样本标准偏差

如何从条带订阅响应对象中正确获取 struct 项?

如何对使用转换器的 Grails 服务进行单元测试?

如何在不消除对象歧义的情况下使用 circe 解码 ADT

通过 JSON 发送 HTML 代码

AJAX 将 JavaScript 字符串数组发布到 JsonResult 作为 List 总是返回 Null?

严重:找不到媒体类型 = 应用程序/json、类型 = 类 com.jersey.jaxb.Todo、通用类型 = 类 com.jersey.jaxb.Todo 的 MessageBodyWriter

如何从 JSON 响应中提取单个值?

如何从 github API 解析链接头