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?

1 Like

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.

5 Likes