chittolina

chittolina

Can't decode JSON

Hey guys! I’m new to Elixir language and I’m trying to perform a simple HTTP request to list StackExchange users, however I’m not able to decode the JSON received by the API with Jason or Poison.

I tried the following to perform the request:

Poison.decode! HTTPoison.get!("https://api.stackexchange.com/users").body

The error I get is:

** (Poison.SyntaxError) Unexpected token at position 0: ^_
    (poison) lib/poison/parser.ex:57: Poison.Parser.parse!/2
    (poison) lib/poison.ex:83: Poison.decode!/2

However, if I hit this URI in the browser, it’s a perfectly valid JSON. Am I missing something?
Could someone please help me here?

Marked As Solved

al2o3cr

al2o3cr

The reply is gzipped:

iex(3)> s = HTTPoison.get!("https://api.stackexchange.com/users")     
%HTTPoison.Response{
  body: <<31, 139, 8, 0, 0, 0, 0, 0, 4, 0, 171, 86, 74, 45, 42, 202, 47, 138,
    207, 76, 81, 178, 50, 49, 48, 208, 129, 114, 115, 83, 139, 139, 19, 211, 83,
    149, 172, 148, 138, 51, 75, 82, 21, 50, 139, 21, 138, 82, 11, ...>>,
  headers: [
    {"Cache-Control", "private"},
    {"Content-Type", "application/json; charset=utf-8"},
    {"Content-Encoding", "gzip"},
    {"Access-Control-Allow-Origin", "*"},
    {"Access-Control-Allow-Methods", "GET, POST"},
    {"Access-Control-Allow-Credentials", "false"},
    {"X-Content-Type-Options", "nosniff"},
    {"Date", "Sun, 27 Oct 2019 03:14:39 GMT"},
    {"Content-Length", "85"}
  ],
  request: %HTTPoison.Request{ 
    body: "",
    headers: [],
    method: :get,
    options: [],
    params: %{},
    url: "https://api.stackexchange.com/users"
  },
  request_url: "https://api.stackexchange.com/users",
  status_code: 400
}

For some reason, handling this has been a requested feature since 2015

Edit: the StackExchange API docs mention that results are ALWAYS compressed (even with Accept-Encoding: identity) so you’ll need to pass the body through :zlib.gunzip/1 or similar before decoding, as mentioned in one of the comments on the issue linked above.

Extra edit: See also this official discussion of why the Accept-Encoding header is ignored.

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement