I'm getting binary in body instead of a json string

Hi. I’m getting binary in json response body instead of a string. It’s probably because the response json is too big.

What can I do in this situation? Or is it a bug with httpoison?

Thanks

iex(17)> {:ok, %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.get "https://url"
{:ok,
 %HTTPoison.Response{body: <<31, 139, 8, 0, 0, 0, 0, 0, 0, 3, 237, 125, 121,
111, 219, 88, 182, 231, 87, 81, 121, 254, 233, 6, 204, 72, 20, 181, 22, 30,
166, 224, 53, 222, 109, 121, 143, 31, 6, 198, 149, 68, 137, 52, 37, 94, 153,
164, ...>>,
  headers: [{"Server", "nginx/1.11.4"},
   {"Date", "Tue, 10 Oct 2017 23:35:36 GMT"},
   {"Content-Type", "application/json; charset=utf-8"},
   {"Transfer-Encoding", "chunked"}, {"Connection", "keep-alive"},
   {"cache-control", "max-age=300"},
   {"x-request-id", "7qcnn88qvaf12tuc2u88nkqoj5odk19v"},
   {"Expires", "Tue, 10 Oct 2017 23:40:36 GMT"}, {"Content-Encoding", "gzip"},
   {"Access-Control-Allow-Origin", "*"}],
  request_url: "https://url",
  status_code: 200}}
iex(18)> body
<<31, 139, 8, 0, 0, 0, 0, 0, 0, 3, 237, 125, 121, 111, 219, 88, 182, 231, 87,
  81, 121, 254, 233, 6, 204, 72, 20, 181, 22, 30, 166, 224, 53, 222, 109, 121,
  143, 31, 6, 198, 149, 68, 137, 52, 37, 94, 153, 164, 100, 83, ...>>
iex(19)> Poison.decode body
{:error, {:invalid, <<31>>, 0}}
iex(20)>
1 Like

Your body is not a string, do you see those 0's in there? Those are invalid string characters, thus it is not JSON that you got but rather some kind of binary blob, there is nothing httpoison could do in this case.

And please please please use markdown, don’t put code in pictures. ^.^;

2 Likes

Ah cool, you’ve posted markdown. :slight_smile:

Well ithe header says the content is gzip encoded, I’m unsure if httpoison auto-decodes that for you, but if not then just gzip decode it first and ‘then’ pipe it in to httpoison I’d bet? :slight_smile:

2 Likes

It worked, thanks. I used :zlib.gunzip(body)
Then it’s decoded normally.

3 Likes