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?
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.
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.
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.
Try this command:
curl -s -D /dev/stderr http://www.website.com/... | jq
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With