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}






















