Comments (4)
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.
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.
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.
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)
- Telemetry support HOT 1
- (HTTPoison.Error) :closed needs a proper message that its from hackney HOT 3
- 1.8 not installing HOT 1
- Response HOT 2
- Request is getting closed. HOT 2
- HTTPoison.MaybeRedirect for 308 status_code ? HOT 11
- why no_proxy doesn't support CIDR range format?
- checkout_failure when using httpoison with proxy HOT 3
- Lowercase location header not recognized when constructing %HTTPoison.MaybeRedirect{}
- TLS client: In state wait_cert_cr at ssl_handshake.erl:1984 generated CLIENT ALERT: Fatal - Unknown CA HOT 3
- SSL options replace default options (instead of just being added to them) HOT 2
- Default TLS version? HOT 2
- Proxy option not working HOT 1
- Hackney 1.18? HOT 1
- Overly loose typespecs
- HTTPoison.get! follow_redirect does not work with an async request HOT 1
- Nested form-encoded params don't work HOT 2
- "Path not supported by Algolia REST API" HOT 2
- How to contribute HOT 1
- ipv6 localhost address is passed to hackney without square brackets HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from httpoison.