-
-
Notifications
You must be signed in to change notification settings - Fork 871
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
Event hooks do not allow for mutation of original response #1343
Comments
You might want to check out custom transports which give you this kind of control https://www.python-httpx.org/advanced/#custom-transports Also see the discussion regarding adding Middleware functionality #345 |
@coltoneakins Hi, Yup this aspect is definitely by design. Event hooks are meant for tapping into requests and responses as they flow through an application, but they're not meant to change that flow in any meaningful way. If you'd like to gain that kind of control, then I'd suggest you look at resources @johtso linked to. I'll admit we're aware there's some ironing out required from a UX perspective, but we're slowly getting there and I hope we'll soon be able to provide some more compelling examples of how custom transports can be used to alter / enhance request flows. Closing for now; if you're running into issues feel free to reach back, or head to the chat so we can discuss anything you'd see as a blocker. :) |
Oh and actually, if this isn't something we have in our Requests compatibility guide already, I assume we'd gladly accept a PR adding there that event hooks aren't designed to allow mutating responses or performing extra requests, linking to our custom transports docs as the best alternative. |
* Updates compatibility guide to address event hooks In `requests`, event hook callbacks can mutate response/request objects. In HTTPX, this is not the case. Added text to address this difference, and added a link to the best alternate HTTPX offers in this circumstance. More context: #1343 (comment) * Apply suggestions from code review Co-authored-by: Florimond Manca <[email protected]> Co-authored-by: Florimond Manca <[email protected]>
* Updates compatibility guide to address event hooks In `requests`, event hook callbacks can mutate response/request objects. In HTTPX, this is not the case. Added text to address this difference, and added a link to the best alternate HTTPX offers in this circumstance. More context: encode/httpx#1343 (comment) * Apply suggestions from code review Co-authored-by: Florimond Manca <[email protected]> Co-authored-by: Florimond Manca <[email protected]>
Checklist
master
.Describe the bug
Event hooks do not facilitate mutating the original response/request for which the hook is attached to. The problem is two-fold:
response.text
. Raises"AttributeError: can't set attribute"
.(I realize this may be 'by-design' to avoid side-effects--like mutations (i.e. to make programming with httpx more 'functional'). But, I am submitting this bug anyway.)
To reproduce
The point of this code sample is to set up something like 'retries' or a 'HTTP interceptor'. If we get a 401 for any request made using the httpx client, try logging in again and re-send the original request.
However for the sake of this bug report, I made this retry whenever a 404 response was received from the API. Just to illustrate the bug.
Set up an environment with pipenv and install httpx.
Use this code sample:
Expected behavior
The original response to the 404 resource is replaced with a 200 response to another resource--because that is how the hook is designed to work (i.e. with the ability to mutate the original response or substitute another one).
Actual behavior
Hooks cannot mutate nor replace the original response object given to them. In the code sample above, not all properties of the Response instance can be mutated. Also, the Response instance cannot be replaced because event hooks in httpx do not return a value (unlike in Requests for Python).
Debugging material
Code gives
Not found
despite attempting to return a 200 resource response.
Environment
Additional context
How to Fix an Issue Like This
response.text
found here in the httpx source code: linkThe text was updated successfully, but these errors were encountered: