Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print response headers to console and save content to file?

Tags:

httpie

How can I print the response headers to console and save the response content to a file?


I tried

http GET http://download.sysinternals.com/files/SysinternalsSuite.zip --output sis.zip

But this printed both the headers and content to the file, making it nonsense.

like image 439
Colonel Panic Avatar asked Oct 06 '12 21:10

Colonel Panic


2 Answers

It is possible in an updated version, the --output option will print the output into a file, and the --download option will make sure that the headers are print to the console, and only the content to the output file.

Here is an example:

http GET "https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=Stack%20Overflow" --download --output bla.txt
like image 60
yellowcap Avatar answered Oct 13 '22 17:10

yellowcap


This is what HTTPie does by default:

  • When the output is not redirected, the whole message is printed to the terminal (headers & body). However, binary data is not printed to the terminal.
  • When the output is redirected to a file, then only the body is printed (headers & body). It doesn't matter whether the body is binary or not.

So, the output depends on where it is being printed to. You can overwrite the default context-sensitive behaviour with one of the output options. For example, the following saves the headers as well as the body to a file:

http --output sis.zip --print=hb download.sysinternals.com/files/SysinternalsSuite.zip 

HTTPie currently doesn't allow outputing binary data to the terminal.

like image 28
Jakub Roztocil Avatar answered Oct 13 '22 16:10

Jakub Roztocil