kirqe

kirqe

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)>

Most Liked

kirqe

kirqe

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

OvermindDL1

OvermindDL1

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. ^.^;

OvermindDL1

OvermindDL1

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:

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement