剥离http响应并仅获取CURL中的内容

时间:2014-06-04 18:09:02

标签: bash curl

我使用以下命令从服务器获取文件:

curl -i -L --user user:pass -o $s.po -F file=@$s -X GET http://address

然而,结果如下:

HTTP/1.1 100 Continue

HTTP/1.1 200 OK
Server: nginx/1.2.1
Date: Wed, 04 Jun 2014 18:00:08 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Content-Language: en
Expires: Wed, 04 Jun 2014 18:00:08 GMT
Vary: Authorization, Host, Accept-Language, Cookie
Last-Modified: Wed, 04 Jun 2014 18:00:08 GMT
Cache-Control: max-age=0
X-Frame-Options: SAMEORIGIN
Strict-Transport-Security: max-age=15768000; includeSubDomains
X-UA-Compatible: IE=edge,chrome=1

{
    "content": "foo\nbar", 
    "mimetype": "text/x-po"
}

我只想编写响应的foo\nbar部分,以将完全相同的文件存储到磁盘中。我怎么能这样做?

删除-i选项并添加> output.json后,文件$s.po中会显示以下内容:

{
    "content": "foo\nbar", 
    "mimetype": "text/x-po"
}

1 个答案:

答案 0 :(得分:1)

删除-i选项并将输出管道输出到awk命令并保存到文件中:

curl -L --user user:pass -o $s.po -F file=@$s -X GET http://address |
  awk -F'[:,]' '$1~/content/{gsub(/[" ]/, "", $2); print $2}' > output.json

根据man curl

  

-i, --include

     

(HTTP)在输出中包含HTTP标头。 HTTP标头包含类似的内容   服务器名称,文档日期,HTTP版本等......