sahilpaudel

sahilpaudel

Unable to send headers in HTTPoison.get

My code snippet.

def make_request(path, params) do
    url = construct_url(path, params)
    headers = []

    case HTTPoison.get(url, headers) do
      {:ok, %HTTPoison.Response{status_code: 200, body: body}} -> 
        case Poison.decode(body) do
          {:ok, decoded} -> {:ok, decoded}
          {:error, error} ->{:error, error}
        end
      {:ok,  %HTTPoison.Response{status_code: status_code }} -> 
          {:error, status_code}
      {:error, error} -> {:error, error}
    end
  
  end

  defp construct_url(path, params) do
    path <> params
  end

the above code is working fine as expected however if I add

headers = [{“Accept-Encoding”,“gzip, deflate”}, {“Content-Type”, “application/json”}]

It gives error

{:error, {:invalid, <<31>>, 0}}

Anything I am doing it wrong here

Marked As Solved

Jacek

Jacek

Then you can’t use simple Poison.decode(body) since Poison expects string whereas what you pass in is its compressed version. Try to decompress it first, sth like :zlib.gunzip(body), however first you should probably check if the response includes the following header: Content-Encoding: gzip

Also Liked

speeddragon

speeddragon

I think that issue could be about Poison.decode/1? Not sure if HTTPoison uncompresses the body. If not, you need to decompress it before you parse it, because ASCII 31 isn’t a valid string character. Not sure if this is the case.

The first 32 characters in the ASCII-table are unprintable control codes and are used to control peripherals such as printers.
https://www.ascii-code.com/

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
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
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
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
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
nobody
Hi! In PHP: $SERVER['SERVERADDR'] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42842 311
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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
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
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I fore...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31107 143
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

We're in Beta

About us Mission Statement