Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTPPoison.Base process_response_body with maybe gziped headers #345

Closed
favetelinguis opened this issue Aug 12, 2018 · 2 comments
Closed

Comments

@favetelinguis
Copy link

Is there a way one can check the headers if the message is gziped in:

defp process_response_body(body), do: body

in my case i can not assume they are always gziped so I need to check the response headers if its gziped or not but I cant figure out how to do this with HTTPPoison.Base?

@steffkes
Copy link

I had a similar case and i'd say it's not possible. response calls those process_* functions only with a single argument:

%Response{
status_code: process_status_code.(status_code),
headers: process_headers.(headers),
body: process_response_body.(body),
request_url: request_url
}}

which means, you have either access to headers or body but not both. @edgurgel i'd guess that's basically just what was existing back then (i've traced it back to a commit from three years ago). i can't say w/o further looking how much of a change that would be, if:

We setup the Response and then pass it through one/some function(s) which would be allowed to transform the complete response? In that case, one could execute conditional stuff, like @favetelinguis asked for and for example decode json only if the content-type says so.

Same would be true for the Request. Wouldn't be the first time i'd like to have conditional encoding of the request body depending on the headers.

@ryanwinchester
Copy link
Contributor

You should be able to use process_response/1 now in master. It is the last thing called on the response and if you have one piece that depends on another, is probably the best thing to use for that currently.

defmodule MyThing do
  use HTTPoison.Base

  alias HTTPoison.Response

  def process_response(response) do
    %Response{response |
      body: body_processor(response.headers, response.body)
    }
  end

  # contrived example
  defp body_processor(headers, body) do
    if headers, do: body, else: ""
  end
end

@edgurgel edgurgel closed this as completed Jul 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants