Skip to content
Myr

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

5 files+36 added−1 removed

modified:docs/api.md+2 added-1 removed
modified:docs/compatibility.md+14 added
113 unmodified lines
114115## Query Parameters116117`requests` omits `params` whose values are `None` (e.g. `requests.get(..., params={"foo": None})`). This is not supported by HTTPX.118119## Determining the next redirect request120121When using `allow_redirects=False`, the `requests` library exposes an attribute `response.next`, which can be used to obtain the next redirect request.122123In HTTPX, this attribute is instead named `response.next_request`. For example:124125```python126client = httpx.Client()127request = client.build_request("GET", ...)128while 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 lines
831 request = self._build_redirect_request(request, response)832 history = history + [response]833834 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 lines
1475 request = self._build_redirect_request(request, response)1476 history = history + [response]14771478 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 lines
modified:httpx/_models.py+4 added
874 unmodified lines
875 self.headers = Headers(headers)876877 self._request: typing.Optional[Request] = request878879 # When allow_redirects=False and a redirect is received,880 # the client will set `response.next_request`.881 self.next_request: typing.Optional[Request] = None882883 self.call_next: typing.Optional[typing.Callable] = None884885 self.ext = {} if ext is None else ext886 self.history = [] if history is None else list(history)
586 unmodified lines
modified:tests/client/test_redirects.py+14 added
164 unmodified lines
165 assert response.is_redirect is False166 assert len(response.history) == 1167168169def 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 None176177 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 None181182183def test_head_redirect():184 """185 Contrary to Requests, redirects remain enabled by default for HEAD requests.186 """
244 unmodified lines