www
www

Reputation: 596

Stripping http response and getting only contents in CURL

I am using the following command to fetch a file from a server:

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

However, the result is like this:

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"
}

I would like to write only the foo\nbar part of the response to store exactly the same file into the disk. How can I do that?

After removing -i option and adding > output.json gives the following output in the file $s.po:

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

Upvotes: 0

Views: 3425

Answers (1)

anubhava
anubhava

Reputation: 785068

Remove -i option and pipe output to an awk command and save in a file:

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

As per man curl:

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

Upvotes: 1

Related Questions