Update Response.raise_for_status() to respect follow_redirects parameter#3631
Update Response.raise_for_status() to respect follow_redirects parameter#3631him2994 wants to merge 2 commits intoencode:masterfrom
Response.raise_for_status() to respect follow_redirects parameter#3631Conversation
follow_redirects parameter to Response.raise_for_status() method
follow_redirects parameter to Response.raise_for_status() methodResponse.raise_for_status() to respect follow_redirects parameter
|
I think this is a breaking change. It also doesn't feel right to treat 3xx status codes as successful responses; only 2xx codes indicate success. Perhaps extending raise_for_status to accept a boolean argument like allow_redirects=True would be better, in my opinion? P.S. I am just a random user; I may be completely wrong. |
|
@Zaczero
This is NOT a breaking change because the new behaviour only kicks in when developers explicitly opt in by setting I chose this approach because the behaviour automatically matches the request's redirect handling preference. Thanks for looking into this. |
|
Hey!
follow_redirects=False is the default, so my understanding is that the intent is "I want to handle ready-to-use responses." Redirect responses do not contain actual data and are not ready-to-use.
In the past I simply checked for redirects before raise_for_status and it worked okay (I used the .is_redirect property). What's inconsistent?
follow_redirects: bool = False is the default when unset in AsyncClient's and Client's __init__. |
|
Thanks for the Correction I'd like to rethink whether there's a problem here that needs solving, or if the current API is already working as intended. For now, closing the PR. |
Summary
This PR enhances the
Response.raise_for_status()method to detect when redirect responses (3xx status codes) automatically should not raise exceptions. When a request is made withfollow_redirects=False, HTTPX setsresponse.next_requestfor redirect responses, andraise_for_status()now intelligently detects this and treats redirects as successful responses instead of raisingHTTPStatusError.This change addresses the inconsistency where developers had to manually check
response.next_requestor implement custom logic to handle redirects whenfollow_redirects=Falsewas set on the request.Current Behaviour
raise_for_status()always raisesHTTPStatusErrorfor 3xx redirect responsesresponse.next_requestmanuallyExpected Behaviour
raise_for_status()automatically detects when redirects shouldn't raise exceptionsfollow_redirects=Falseis set on the request, redirect responses are treated as successfulChanges Made
-
httpx/_models.py: Modifiedraise_for_status()method to check fornext_requestpresence and return early for redirects whenfollow_redirects=Falsetests/models/test_responses.py: Added test case to verify that redirect responses don't raise exceptions whenfollow_redirects=Falsedocs/quickstart.md: Updated documentation to explain the new redirect handling behaviour with clear examplesChecklist
[x] I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
[x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
[x] I've updated the documentation accordingly.