Add response.next_request (#1334)
* Add response.next_request * Add response.next_request * Add response.next_request to the docs
This commit is contained in:
parent
32d37cfdf1
commit
3f51392bea
@ -31,7 +31,7 @@
|
||||
|
||||
::: httpx.delete
|
||||
:docstring:
|
||||
|
||||
|
||||
::: httpx.stream
|
||||
:docstring:
|
||||
|
||||
@ -63,6 +63,7 @@
|
||||
* `.encoding` - **str**
|
||||
* `.is_redirect` - **bool**
|
||||
* `.request` - **Request**
|
||||
* `.next_request` - **Optional[Request]**
|
||||
* `.cookies` - **Cookies**
|
||||
* `.history` - **List[Response]**
|
||||
* `.elapsed` - **[timedelta](https://docs.python.org/3/library/datetime.html)**
|
||||
|
||||
@ -115,3 +115,17 @@ On the other hand, HTTPX uses [HTTPCore](https://github.com/encode/httpcore) as
|
||||
## Query Parameters
|
||||
|
||||
`requests` omits `params` whose values are `None` (e.g. `requests.get(..., params={"foo": None})`). This is not supported by HTTPX.
|
||||
|
||||
## Determining the next redirect request
|
||||
|
||||
When using `allow_redirects=False`, the `requests` library exposes an attribute `response.next`, which can be used to obtain the next redirect request.
|
||||
|
||||
In HTTPX, this attribute is instead named `response.next_request`. For example:
|
||||
|
||||
```python
|
||||
client = httpx.Client()
|
||||
request = client.build_request("GET", ...)
|
||||
while request is not None:
|
||||
response = client.send(request, allow_redirects=False)
|
||||
request = response.next_request
|
||||
```
|
||||
|
||||
@ -832,6 +832,7 @@ class Client(BaseClient):
|
||||
history = history + [response]
|
||||
|
||||
if not allow_redirects:
|
||||
response.next_request = request
|
||||
response.call_next = functools.partial(
|
||||
self._send_handling_redirects,
|
||||
request=request,
|
||||
@ -1475,6 +1476,7 @@ class AsyncClient(BaseClient):
|
||||
history = history + [response]
|
||||
|
||||
if not allow_redirects:
|
||||
response.next_request = request
|
||||
response.call_next = functools.partial(
|
||||
self._send_handling_redirects,
|
||||
request=request,
|
||||
|
||||
@ -876,6 +876,10 @@ class Response:
|
||||
|
||||
self._request: typing.Optional[Request] = request
|
||||
|
||||
# When allow_redirects=False and a redirect is received,
|
||||
# the client will set `response.next_request`.
|
||||
self.next_request: typing.Optional[Request] = None
|
||||
|
||||
self.call_next: typing.Optional[typing.Callable] = None
|
||||
|
||||
self.ext = {} if ext is None else ext
|
||||
|
||||
@ -166,6 +166,20 @@ def test_disallow_redirects():
|
||||
assert len(response.history) == 1
|
||||
|
||||
|
||||
def test_next_request():
|
||||
client = httpx.Client(transport=MockTransport(redirects))
|
||||
request = client.build_request("POST", "https://example.org/redirect_303")
|
||||
response = client.send(request, allow_redirects=False)
|
||||
assert response.status_code == httpx.codes.SEE_OTHER
|
||||
assert response.url == "https://example.org/redirect_303"
|
||||
assert response.next_request is not None
|
||||
|
||||
response = client.send(response.next_request, allow_redirects=False)
|
||||
assert response.status_code == httpx.codes.OK
|
||||
assert response.url == "https://example.org/"
|
||||
assert response.next_request is None
|
||||
|
||||
|
||||
def test_head_redirect():
|
||||
"""
|
||||
Contrary to Requests, redirects remain enabled by default for HEAD requests.
|
||||
|
||||
Loading…
Reference in New Issue
Block a user