Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions httpx/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ def __init__(
timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,
follow_redirects: bool = False,
max_redirects: int = DEFAULT_MAX_REDIRECTS,
keep_method_for_redirects: bool = False,
event_hooks: None | (typing.Mapping[str, list[EventHook]]) = None,
base_url: URL | str = "",
trust_env: bool = True,
Expand All @@ -212,6 +213,7 @@ def __init__(
self._timeout = Timeout(timeout)
self.follow_redirects = follow_redirects
self.max_redirects = max_redirects
self.keep_method_for_redirects = keep_method_for_redirects
self._event_hooks = {
"request": list(event_hooks.get("request", [])),
"response": list(event_hooks.get("response", [])),
Expand Down Expand Up @@ -499,9 +501,20 @@ def _redirect_method(self, request: Request, response: Response) -> str:
method = request.method

# https://tools.ietf.org/html/rfc7231#section-6.4.4
# RFC states that "a user agent can perform a retrieval request".
# Thus override the method regardless of the keep_method_for_redirects
# option
if response.status_code == codes.SEE_OTHER and method != "HEAD":
method = "GET"

# Request method may be changed only for 301 and 302
if response.status_code not in (codes.FOUND, codes.MOVED_PERMANENTLY):
return method

# ... but avoid that in case user requested keeping the current method
if self.keep_method_for_redirects:

@imbdb imbdb Sep 12, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If you are targeting 301/302, Please make them explicit in the condition and also add a comment for this behavior change for future humans/agents to understand the context.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I've added a few logics/comments to explain the intention and hope that addresses your concern.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

می‌خوام برنامه دانلود کنم

return method

# Do what the browsers do, despite standards...
# Turn 302s into GETs.
if response.status_code == codes.FOUND and method != "HEAD":
Expand Down Expand Up @@ -622,9 +635,13 @@ class Client(BaseClient):
* **proxy** - *(optional)* A proxy URL where all the traffic should be routed.
* **timeout** - *(optional)* The timeout configuration to use when sending
requests.
* **follow_redirects** - *(optional)* Follow redirects and send a new
* request to the redirected url automatically.
* **limits** - *(optional)* The limits configuration to use.
* **max_redirects** - *(optional)* The maximum number of redirect responses
that should be followed.
* **keep_method_for_redirects* - *(optional)* Keep the original HTTP method
when following redirects. This is effective only for 301 and 302 .
* **base_url** - *(optional)* A URL to use as the base when building
request URLs.
* **transport** - *(optional)* A transport class to use for sending requests
Expand Down Expand Up @@ -654,6 +671,7 @@ def __init__(
follow_redirects: bool = False,
limits: Limits = DEFAULT_LIMITS,
max_redirects: int = DEFAULT_MAX_REDIRECTS,
keep_method_for_redirects: bool = False,
event_hooks: None | (typing.Mapping[str, list[EventHook]]) = None,
base_url: URL | str = "",
transport: BaseTransport | None = None,
Expand All @@ -667,6 +685,7 @@ def __init__(
timeout=timeout,
follow_redirects=follow_redirects,
max_redirects=max_redirects,
keep_method_for_redirects=keep_method_for_redirects,
event_hooks=event_hooks,
base_url=base_url,
trust_env=trust_env,
Expand Down Expand Up @@ -1336,9 +1355,13 @@ class AsyncClient(BaseClient):
* **proxy** - *(optional)* A proxy URL where all the traffic should be routed.
* **timeout** - *(optional)* The timeout configuration to use when sending
requests.
* **follow_redirects** - *(optional)* Follow redirects and send a new
* request to the redirected url automatically.
* **limits** - *(optional)* The limits configuration to use.
* **max_redirects** - *(optional)* The maximum number of redirect responses
that should be followed.
* **keep_method_for_redirects* - *(optional)* Keep the original HTTP method
when following redirects. This is effective only for 301 and 302 .
* **base_url** - *(optional)* A URL to use as the base when building
request URLs.
* **transport** - *(optional)* A transport class to use for sending requests
Expand Down Expand Up @@ -1367,6 +1390,7 @@ def __init__(
follow_redirects: bool = False,
limits: Limits = DEFAULT_LIMITS,
max_redirects: int = DEFAULT_MAX_REDIRECTS,
keep_method_for_redirects: bool = False,
event_hooks: None | (typing.Mapping[str, list[EventHook]]) = None,
base_url: URL | str = "",
transport: AsyncBaseTransport | None = None,
Expand All @@ -1381,6 +1405,7 @@ def __init__(
timeout=timeout,
follow_redirects=follow_redirects,
max_redirects=max_redirects,
keep_method_for_redirects=keep_method_for_redirects,
event_hooks=event_hooks,
base_url=base_url,
trust_env=trust_env,
Expand Down
36 changes: 36 additions & 0 deletions tests/client/test_redirects.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ def test_redirect_301():
response = client.post("https://example.org/redirect_301", follow_redirects=True)
assert response.status_code == httpx.codes.OK
assert response.url == "https://example.org/"
assert response.request.method == "GET"
assert len(response.history) == 1


def test_redirect_301_keep_method():
client = httpx.Client(
transport=httpx.MockTransport(redirects), keep_method_for_redirects=True
)
response = client.post("https://example.org/redirect_301", follow_redirects=True)
assert response.status_code == httpx.codes.OK
assert response.url == "https://example.org/"
assert response.request.method == "POST"
assert len(response.history) == 1


Expand All @@ -126,6 +138,18 @@ def test_redirect_302():
response = client.post("https://example.org/redirect_302", follow_redirects=True)
assert response.status_code == httpx.codes.OK
assert response.url == "https://example.org/"
assert response.request.method == "GET"
assert len(response.history) == 1


def test_redirect_302_keep_method():
client = httpx.Client(
transport=httpx.MockTransport(redirects), keep_method_for_redirects=True
)
response = client.post("https://example.org/redirect_302", follow_redirects=True)
assert response.status_code == httpx.codes.OK
assert response.url == "https://example.org/"
assert response.request.method == "POST"
assert len(response.history) == 1


Expand All @@ -134,6 +158,18 @@ def test_redirect_303():
response = client.get("https://example.org/redirect_303", follow_redirects=True)
assert response.status_code == httpx.codes.OK
assert response.url == "https://example.org/"
assert response.request.method == "GET"
assert len(response.history) == 1


def test_redirect_303_keep_method():
client = httpx.Client(
transport=httpx.MockTransport(redirects), keep_method_for_redirects=True
)
response = client.get("https://example.org/redirect_303", follow_redirects=True)
assert response.status_code == httpx.codes.OK
assert response.url == "https://example.org/"
assert response.request.method == "GET"
assert len(response.history) == 1


Expand Down
Loading