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
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








