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

JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New

Other popular topics Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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...
273 38985 115
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
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 record...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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

We're in Beta

About us Mission Statement