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

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
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
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
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
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
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
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
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

We're in Beta

About us Mission Statement