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

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
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
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
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
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
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
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
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement