Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Report HTTP Response Headers to stderr?

Tags:

curl

I have been using curl -i http://website.com for a long time. It's great, it reports the response headers and information.

I also use a tool jq which parses and pretty prints JSON. I'd like to do the following:

curl -i http://website.com | jq -r .

The problem with this is that the HTTP headers are forwarded to jq.

Is there a way to redirect -i to standard error?

like image 340
Naftuli Kay Avatar asked Mar 01 '18 19:03

Naftuli Kay


People also ask

How do I print response headers in curl?

In default mode, curl doesn't display request or response headers, only displaying the HTML contents. To display both request and response headers, we can use the verbose mode curl -v or curl -verbose . In the resulting output: The lines beginning with > indicate request headers.

Does HTTP response have headers?

A response header is an HTTP header that can be used in an HTTP response and that doesn't relate to the content of the message. Response headers, like Age , Location or Server are used to give a more detailed context of the response.

How do you send a header through curl?

To send an HTTP header with a Curl request, you can use the -H command-line option and pass the header name and value in "Key: Value" format. If you do not provide a value for the header, this will remove the standard header that Curl would otherwise send. The number of HTTP headers is unlimited.


2 Answers

Try this command:

curl -s -D /dev/stderr http://www.website.com/... | jq

like image 192
Zmey Avatar answered Oct 19 '22 02:10

Zmey


Since I faced the some problem today, I came up using:

curl -i http://some-server/get.json | awk '{ sub("\r$", ""); print }' | awk -v RS= 'NR==1{print > "/dev/stderr";next} 1' > /dev/stdout  | jq .

Most likely not the best solution, but it works for me.

Explanation: the first awk program will just convert windows new lines to unix new lines.

In the second program -v RS= will instruct awk to use one or more blank lines as record separators[1]. NR==1{print > "/dev/stderr";next} will print the first record (NR==1) to stderr, the next statement forces awk to immediately stop processing the current record and go on to the next record[2]. 1 is just a short hand for {print $0}[3].

[1]https://stackoverflow.com/a/33297878
[2]https://www.gnu.org/software/gawk/manual/html_node/Next-Statement.html
[3]https://stackoverflow.com/a/20263611

like image 3
jakrol Avatar answered Oct 19 '22 03:10

jakrol