Giter Site home page Giter Site logo

Comments (4)

edgurgel avatar edgurgel commented on May 3, 2024

I understand the issue and I'm looking for a reasonable way of doing this, but there's one small problem with this approach. On async requests you do not receive the body and headers at the same time: https://github.com/edgurgel/httpoison/blob/master/lib/httpoison/base.ex#L38-L49

We gotta find a solution that won't be inconsistent with the way that async requests work. One option is to process the response by yourself without touching headers and body so you can probably do things like:

case HTTPoison.get(....) do
  %HTTPoison.Response{body: body, headers: %{"Content-Type" => "application/json"} -> do_something_json(body)
  %HTTPoison.Response{body: body, headers: %{"Content-Type" => "application/xml"} -> do_something_xml(body)
end

One possible option could be something like this:

defp process_headers(body, state \\ nil) do
  ...
  new_state
end

defp process_response_body(body, state \\ nil) do
  #Do something on the body based on the new_state
  ...
end

process_headers would be called, and the value returned would be passed to the process_response_body as state

I'm afraid this is a complicated solution and I'm not sure how this would behave with previous implementations as it would have a callback with a different arity.

What do you think?

from httpoison.

xtagon avatar xtagon commented on May 3, 2024

For asynchronous requests, even though headers and body are not processed at the same time, won't the headers always be processed before the body? They come first in the HTTP protocol.

from httpoison.

edgurgel avatar edgurgel commented on May 3, 2024

Yeah, I hope hackney always sends the header first :P.

So the idea is to pass the same headers to process_response_body? Or get the processed headers and feed to process_body on the 2nd argument?

processed_headers = process_headers(headers)
processed_body      = process_response_body(body, processed_headers)

# => %HTTPoison.Response{body: processed_body, headers: processed_headers, ...}

This seems to be the easier way to do this.

from httpoison.

xtagon avatar xtagon commented on May 3, 2024

Well, personally I don't care whether the headers passed are processed or not, because I'm not processing them. But your last example probably makes the most sense.

from httpoison.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.