If I try:
url = "https://www.economist.com/news/finance-and-economics/21727073-economists-struggle-work-out-how-much-free-economy-comes-cost"
{:ok, %HTTPoison.Response{status_code: 200, body: body}} = HTTPoison.get(url)
IO.binwrite body
I see garbled text (instead of html) in the console. But if I view source on the webpage, I see html there. What am I doing wrong?
PS: it works fine with a js http client (axios.js), not sure why it doesn't work with httpoison
That URL returns the body in gzipped form and indicates this by sending the header
Content-Encoding: gzip
.hackney
, the library HTTPoison is built on, does not automatically decode this. This feature will likely be added at some point. Until then, you can decode the body yourself using the:zlib
module if theContent-Encoding
isgzip
: