How to pipe differently the body of the curl answer and the printed output

curl

I would like to print in the command line some output of curl, like the http headers, followed by the body of the answer processed by a stdin/stdout program. For instance:

Print the status code:
curl -s -w "%{http_code} \\n" -o "/dev/null" http://myURL.com

And then process the output with a json parsing tool:
curl -s http://myURL.com | python -mjson.tool

I would like to do both with one command, and I have the feeling that it may be possible thanks to the -o option that makes the difference between the output of curl and the actual answer from the query. The problem is that -o writes directly to a file. Somebody's got a hack?

Best Answer

You can use bash's process substitution >(cmd). For example:

curl -s -w "%{http_code}\n" -o >(ruby -ne 'printf("-%6s%s", $., $_)') http://www.example.com/

Another possibility that may suit your needs is curl -v, which will send the headers to stderr and the body to stdout. E.g.,

curl -v http://www.example.com/ | ruby -ne 'printf("-%6s%s", $., $_)'