Add `response.next_request` (#1334)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Add response.next_request * Add response.next_request * Add response.next_request to the docs
authored by Tom Christie · committed by GitHub · parent 32d37cf · Browse files · 5 files·+36 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+36 added−1 removed
- tests:+14 added−0 removed
- other:+22 added−1 removed
Provenance
- Commit
3f51392- Tree
5e0725b- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
32d37cf
5 files changed+36 added−1 removed
modified:docs/api.md+2 added-1 removed
29 unmodified lines3031323334343536373823 unmodified lines62636465666768697091 unmodified lines29 unmodified lines :docstring:
::: httpx.delete :docstring:
::: httpx.stream :docstring:
## `Client`23 unmodified lines* `.text` - **str*** `.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)** * The amount of time elapsed between sending the request and calling `close()` on the corresponding response received for that request.91 unmodified linesmodified:docs/compatibility.md+14 added
113 unmodified lines114 115 ## Query Parameters116 117 `requests` omits `params` whose values are `None` (e.g. `requests.get(..., params={"foo": None})`). This is not supported by HTTPX.118+119+## Determining the next redirect request120+121+When using `allow_redirects=False`, the `requests` library exposes an attribute `response.next`, which can be used to obtain the next redirect request.122+123+In HTTPX, this attribute is instead named `response.next_request`. For example:124+125+```python126+client = httpx.Client()127+request = client.build_request("GET", ...)128+while request is not None:129+ response = client.send(request, allow_redirects=False)130+ request = response.next_request131+```modified:httpx/_client.py+2 added
830 unmodified lines831 request = self._build_redirect_request(request, response)832 history = history + [response]833 834 if not allow_redirects:835+ response.next_request = request836 response.call_next = functools.partial(837 self._send_handling_redirects,838 request=request,839 timeout=timeout,635 unmodified lines1475 request = self._build_redirect_request(request, response)1476 history = history + [response]1477 1478 if not allow_redirects:1479+ response.next_request = request1480 response.call_next = functools.partial(1481 self._send_handling_redirects,1482 request=request,1483 timeout=timeout,360 unmodified linesmodified:httpx/_models.py+4 added
874 unmodified lines875 self.headers = Headers(headers)876 877 self._request: typing.Optional[Request] = request878 879+ # When allow_redirects=False and a redirect is received,880+ # the client will set `response.next_request`.881+ self.next_request: typing.Optional[Request] = None882+883 self.call_next: typing.Optional[typing.Callable] = None884 885 self.ext = {} if ext is None else ext886 self.history = [] if history is None else list(history)586 unmodified linesmodified:tests/client/test_redirects.py+14 added
164 unmodified lines165 assert response.is_redirect is False166 assert len(response.history) == 1167 168 169+def test_next_request():170+ client = httpx.Client(transport=MockTransport(redirects))171+ request = client.build_request("POST", "https://example.org/redirect_303")172+ response = client.send(request, allow_redirects=False)173+ assert response.status_code == httpx.codes.SEE_OTHER174+ assert response.url == "https://example.org/redirect_303"175+ assert response.next_request is not None176+177+ response = client.send(response.next_request, allow_redirects=False)178+ assert response.status_code == httpx.codes.OK179+ assert response.url == "https://example.org/"180+ assert response.next_request is None181+182+183 def test_head_redirect():184 """185 Contrary to Requests, redirects remain enabled by default for HEAD requests.186 """244 unmodified lines