teofilosalgado

teofilosalgado

Filtering Content-Length and status from HTTPoison AsyncHeaders

Hi all!
Recently I’ve been rewriting an old python downloader utility script in Elixir, but currently I am stuck in response header filtering.

I am starting my HTTPoison in a GenServer worker with:

    options = [
      stream_to: self(),
      timeout: :infinity,
      hackney: [:insecure, recv_timeout: :infinity]
    ]

    worker = HTTPoison.get(url, %{}, options)

then, I threat the response messages with a simple approach:

case message do
      %HTTPoison.AsyncHeaders{headers: headers} ->
        # Code I need goes here

      %HTTPoison.AsyncChunk{chunk: chunk} ->
        IO.binwrite(file, chunk)
        {:noreply, state}

      %HTTPoison.AsyncEnd{} ->
        File.close(file)
        {:stop, :normal, state}

      %HTTPoison.Error{reason: reason} ->
        {:stop, reason, state}

      _ ->
        {:noreply, state}
    end

My problem is that I need to strip from the headers the Content-Length and the status to handle errors like 404 or when my files exceed the specified maximum size. How could I accomplish that with HTTPoison? Am I doing something wrong?

Any help will be very appreciated!
Cheers.

Most Liked

teofilosalgado

teofilosalgado

Well, after some time trying different approaches I finally managed to make it work with:

      %HTTPoison.AsyncStatus{code: code} when code >= 400 ->
        # So, turns out HTTPoison have the very convenient type AsyncStatus!
        {:stop, code, state}

      %HTTPoison.AsyncChunk{chunk: chunk} ->
        IO.binwrite(file, chunk)
        {:noreply, state}

      %HTTPoison.AsyncHeaders{headers: headers} ->
        headers_map = Enum.into(headers, %{})
        # Now I am able to access all headers just like a regular map!
        IO.inspect(headers_map["Content-Length"], label: "Content length: ")
        {:noreply, state}

      %HTTPoison.AsyncEnd{} ->
        File.close(file)
        {:stop, :normal, state}

      %HTTPoison.Error{reason: reason} ->
        {:stop, reason, state}

      _ ->
        {:noreply, state}
axelson

axelson

Scenic Core Team

Welcome to the forum! I’m glad you were able to get something working.

What was the format of headers before the Enum.into call? If it was a Keyword List then you could use Keyword.get(headers, "Content-Length") to get the Content-Length header. A keyword list is a better format to work with for headers since headers can be repeated with different values and you can’t represent that with a map since a map cannot have duplicate keys.

gregvaughn

gregvaughn

It’s a proplist. There’s an erlang module to manage them. For example:
:proplists.get_value("abc", [{"abc", 123}])

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
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
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
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
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
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement