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

Switch follow redirects default #1808

Merged
merged 6 commits into from
Sep 13, 2021
Merged

Conversation

tomchristie
Copy link
Member

Implementation based on discussion #1785.

Switches the auto-redirect behaviour to off by default.

The benefit of this change would be to make it more explicit when redirects should be followed, and avoid code that unintentionally issues multiple requests, as a result of not requesting the correct URL.

We do still allow auto-redirects, but only if explicitly enabled...

client.get(url, follow_redirects=True)

Or, by enabling a client-wide default...

client = httpx.Client(follow_redirects=True)

Also note that we change allow_redirects to follow_redirects.

This is a big change, but I think it may well be one of those points where it's worth us diverging from requests.

It'd also play more neatly into the behaviour that we'd probably want by default from a command-line client, which I'd really like to see on our roadmap.

@tomchristie tomchristie added the api change PRs that contain breaking public API changes label Aug 18, 2021
@tomchristie tomchristie mentioned this pull request Aug 18, 2021
4 tasks
@florimondmanca
Copy link
Member

I think I like the "explicit better than implicit" vibe to this. At the same time I wonder about usability. But isn't there precedent to this anyway? Doesn't cURL ignore redirects and return 3xx by default too? I think so?

Copy link
Member

@florimondmanca florimondmanca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm enjoying the clearer follow_* name as well.

@@ -34,7 +34,7 @@ def request(
auth: AuthTypes = None,
proxies: ProxiesTypes = None,
timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,
allow_redirects: bool = True,
follow_redirects: bool = False,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume we don't consider a soft deprecation (with a warning) to be worth it here, given the amount of code that would require?

@tomchristie
Copy link
Member Author

tomchristie commented Aug 19, 2021

This feels potentially nice, but we need to be careful.

Eg. Potentially it changes what expectations you might want on raise_for_status().

r = httpx.get(...)
r.raise_for_status()  # If 3xx responses *aren't* handled by default, then you might want this to raise exceptions for 3xx cases.
data = r.json()

Urg. Awkward conflicted imperfect design decisions. Ugh.

@tomchristie tomchristie merged commit 47266d7 into master Sep 13, 2021
@tomchristie tomchristie deleted the switch-follow-redirects-default branch September 13, 2021 12:21
bdraco added a commit to bdraco/home-assistant that referenced this pull request Dec 8, 2021
- This is the current behavior in 2021.11 but changed due to a
  breaking change in httpx
  see encode/httpx#1808

- Fixes home-assistant#61239
ikhoon pushed a commit to line/centraldogma-python that referenced this pull request Dec 31, 2021
Motivation
--
- There is [a breaking change](encode/httpx#1808) replacing `allow_redirects` with `follow_redirects` in `httpx`.
- Without changing our test case, it is possible that developers/users can be confused by our code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api change PRs that contain breaking public API changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants