Search code examples
pythonbashhttpie

How to print different results to a screen and to a file in python?


I have noticed that the httpie python tool gives different results in the following two caes:

  1. $ http google.com
  2. $ http google.com > out.txt

The file out.txt misses headers that are present in the first case.


Solution

  • On the manual page of http you can find the following

    Output options:
     --print WHAT, -p WHAT
    
       String specifying what the output should contain:
    
       'H' request headers 'B' request body 'h' response headers 'b' response body
    
       The default behaviour is 'hb' (i.e., the response headers  and  body  is 
       printed), if standard  output  is  not redirected. If the output is piped
       to another program or to a file, then only the response body is printed by
       default.
    

    Which indicates that http intentionally behaves differently whenever the output is redirected. To obtain the same behavior as for not redirected output you can use

    `http --print hb google.com > out.txt`
    

    (But also note that pretty-printing behaves differently with redirection.)