Skip to content
Myr

Switch follow redirects default (#1808)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Switch default on allow_redirects to False * allow_redirects -> follow_redirects * Update follow_redirects default in top-level API * Update docs on follow_redirects

authored by Tom Christie · committed by GitHub · parent 0b4a832 · Browse files · 8 files+218 added−160 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

8 files+218 added−160 removed

  • tests:+53 added−31 removed
  • other:+165 added−129 removed

8 files+218 added−160 removed

modified:docs/compatibility.md+64 added-34 removed
modified:docs/quickstart.md+19 added-17 removed
126 unmodified lines
127128To include additional headers in the outgoing request, use the `headers` keyword argument:129130```pycon131>>> url = 'http://httpbin.org/headers'131>>> url = 'https://httpbin.org/headers'132>>> headers = {'user-agent': 'my-app/0.0.1'}133>>> r = httpx.get(url, headers=headers)134```135
243 unmodified lines
379380Any cookies that are set on the response can be easily accessed:381382```pycon383>>> r = httpx.get('http://httpbin.org/cookies/set?chocolate=chip', allow_redirects=False)383>>> r = httpx.get('https://httpbin.org/cookies/set?chocolate=chip')384>>> r.cookies['chocolate']385'chip'386```387388To include cookies in an outgoing request, use the `cookies` parameter:389390```pycon391>>> cookies = {"peanut": "butter"}392>>> r = httpx.get('http://httpbin.org/cookies', cookies=cookies)392>>> r = httpx.get('https://httpbin.org/cookies', cookies=cookies)393>>> r.json()394{'cookies': {'peanut': 'butter'}}395```396
10 unmodified lines
407```408409## Redirection and History410411By default, HTTPX will follow redirects for all HTTP methods.411By default, HTTPX will **not** follow redirects for all HTTP methods, although412this can be explicitly enabled.413413414The `history` property of the response can be used to inspect any followed redirects.415It contains a list of any redirect responses that were followed, in the order416in which they were made.417414For example, GitHub redirects all HTTP requests to HTTPS.415416```pycon417>>> r = httpx.get('http://github.com/')422>>> r.url423URL('https://github.com/')418>>> r.status_code425200419301420>>> r.history427[<Response [301 Moved Permanently]>]421[]422>>> r.next_request423<Request('GET', 'https://github.com/')>424```425430You can modify the default redirection handling with the allow_redirects parameter:426You can modify the default redirection handling with the `follow_redirects` parameter:427428```pycon433>>> r = httpx.get('http://github.com/', allow_redirects=False)429>>> r = httpx.get('http://github.com/', follow_redirects=True)430>>> r.url431URL('https://github.com/')432>>> r.status_code435301433200434>>> r.history437[]435[<Response [301 Moved Permanently]>]436```437438The `history` property of the response can be used to inspect any followed redirects.439It contains a list of any redirect responses that were followed, in the order440in which they were made.441442## Timeouts443444HTTPX defaults to including reasonable timeouts for all network operations,445meaning that if a connection is not properly established then it should always
39 unmodified lines
modified:httpx/_api.py+19 added-19 removed
32 unmodified lines
33 cookies: CookieTypes = None,34 auth: AuthTypes = None,35 proxies: ProxiesTypes = None,36 timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,37 allow_redirects: bool = True,37 follow_redirects: bool = False,38 verify: VerifyTypes = True,39 cert: CertTypes = None,40 trust_env: bool = True,41) -> Response:
23 unmodified lines
65 request.66 * **proxies** - *(optional)* A dictionary mapping proxy keys to proxy URLs.67 * **timeout** - *(optional)* The timeout configuration to use when sending68 the request.69 * **allow_redirects** - *(optional)* Enables or disables HTTP redirects.69 * **follow_redirects** - *(optional)* Enables or disables HTTP redirects.70 * **verify** - *(optional)* SSL certificates (a.k.a CA bundle) used to71 verify the identity of requested hosts. Either `True` (default CA bundle),72 a path to an SSL certificate file, an `ssl.SSLContext`, or `False`73 (which will disable verification).
32 unmodified lines
106 json=json,107 params=params,108 headers=headers,109 auth=auth,110 allow_redirects=allow_redirects,110 follow_redirects=follow_redirects,111 )112113114@contextmanager
10 unmodified lines
125 cookies: CookieTypes = None,126 auth: AuthTypes = None,127 proxies: ProxiesTypes = None,128 timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,129 allow_redirects: bool = True,129 follow_redirects: bool = False,130 verify: VerifyTypes = True,131 cert: CertTypes = None,132 trust_env: bool = True,133) -> typing.Iterator[Response]:
24 unmodified lines
158 json=json,159 params=params,160 headers=headers,161 auth=auth,162 allow_redirects=allow_redirects,162 follow_redirects=follow_redirects,163 ) as response:164 yield response165166
4 unmodified lines
171 headers: HeaderTypes = None,172 cookies: CookieTypes = None,173 auth: AuthTypes = None,174 proxies: ProxiesTypes = None,175 allow_redirects: bool = True,175 follow_redirects: bool = False,176 cert: CertTypes = None,177 verify: VerifyTypes = True,178 timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,179 trust_env: bool = True,
13 unmodified lines
193 headers=headers,194 cookies=cookies,195 auth=auth,196 proxies=proxies,197 allow_redirects=allow_redirects,197 follow_redirects=follow_redirects,198 cert=cert,199 verify=verify,200 timeout=timeout,201 trust_env=trust_env,
7 unmodified lines
209 headers: HeaderTypes = None,210 cookies: CookieTypes = None,211 auth: AuthTypes = None,212 proxies: ProxiesTypes = None,213 allow_redirects: bool = True,213 follow_redirects: bool = False,214 cert: CertTypes = None,215 verify: VerifyTypes = True,216 timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,217 trust_env: bool = True,
13 unmodified lines
231 headers=headers,232 cookies=cookies,233 auth=auth,234 proxies=proxies,235 allow_redirects=allow_redirects,235 follow_redirects=follow_redirects,236 cert=cert,237 verify=verify,238 timeout=timeout,239 trust_env=trust_env,
7 unmodified lines
247 headers: HeaderTypes = None,248 cookies: CookieTypes = None,249 auth: AuthTypes = None,250 proxies: ProxiesTypes = None,251 allow_redirects: bool = True,251 follow_redirects: bool = False,252 cert: CertTypes = None,253 verify: VerifyTypes = True,254 timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,255 trust_env: bool = True,
13 unmodified lines
269 headers=headers,270 cookies=cookies,271 auth=auth,272 proxies=proxies,273 allow_redirects=allow_redirects,273 follow_redirects=follow_redirects,274 cert=cert,275 verify=verify,276 timeout=timeout,277 trust_env=trust_env,
11 unmodified lines
289 headers: HeaderTypes = None,290 cookies: CookieTypes = None,291 auth: AuthTypes = None,292 proxies: ProxiesTypes = None,293 allow_redirects: bool = True,293 follow_redirects: bool = False,294 cert: CertTypes = None,295 verify: VerifyTypes = True,296 timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,297 trust_env: bool = True,
14 unmodified lines
312 headers=headers,313 cookies=cookies,314 auth=auth,315 proxies=proxies,316 allow_redirects=allow_redirects,316 follow_redirects=follow_redirects,317 cert=cert,318 verify=verify,319 timeout=timeout,320 trust_env=trust_env,
11 unmodified lines
332 headers: HeaderTypes = None,333 cookies: CookieTypes = None,334 auth: AuthTypes = None,335 proxies: ProxiesTypes = None,336 allow_redirects: bool = True,336 follow_redirects: bool = False,337 cert: CertTypes = None,338 verify: VerifyTypes = True,339 timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,340 trust_env: bool = True,
14 unmodified lines
355 headers=headers,356 cookies=cookies,357 auth=auth,358 proxies=proxies,359 allow_redirects=allow_redirects,359 follow_redirects=follow_redirects,360 cert=cert,361 verify=verify,362 timeout=timeout,363 trust_env=trust_env,
11 unmodified lines
375 headers: HeaderTypes = None,376 cookies: CookieTypes = None,377 auth: AuthTypes = None,378 proxies: ProxiesTypes = None,379 allow_redirects: bool = True,379 follow_redirects: bool = False,380 cert: CertTypes = None,381 verify: VerifyTypes = True,382 timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,383 trust_env: bool = True,
14 unmodified lines
398 headers=headers,399 cookies=cookies,400 auth=auth,401 proxies=proxies,402 allow_redirects=allow_redirects,402 follow_redirects=follow_redirects,403 cert=cert,404 verify=verify,405 timeout=timeout,406 trust_env=trust_env,
7 unmodified lines
414 headers: HeaderTypes = None,415 cookies: CookieTypes = None,416 auth: AuthTypes = None,417 proxies: ProxiesTypes = None,418 allow_redirects: bool = True,418 follow_redirects: bool = False,419 cert: CertTypes = None,420 verify: VerifyTypes = True,421 timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,422 trust_env: bool = True,
13 unmodified lines
436 headers=headers,437 cookies=cookies,438 auth=auth,439 proxies=proxies,440 allow_redirects=allow_redirects,440 follow_redirects=follow_redirects,441 cert=cert,442 verify=verify,443 timeout=timeout,444 trust_env=trust_env,
1 unmodified line
modified:httpx/_client.py+62 added-58 removed
163 unmodified lines
164 params: QueryParamTypes = None,165 headers: HeaderTypes = None,166 cookies: CookieTypes = None,167 timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,168 allow_redirects: bool = True,168 follow_redirects: bool = False,169 max_redirects: int = DEFAULT_MAX_REDIRECTS,170 event_hooks: typing.Mapping[str, typing.List[typing.Callable]] = None,171 base_url: URLTypes = "",172 trust_env: bool = True,
6 unmodified lines
179 self._params = QueryParams(params)180 self.headers = Headers(headers)181 self._cookies = Cookies(cookies)182 self._timeout = Timeout(timeout)183 self.allow_redirects = allow_redirects183 self.follow_redirects = follow_redirects184 self.max_redirects = max_redirects185 self._event_hooks = {186 "request": list(event_hooks.get("request", [])),187 "response": list(event_hooks.get("response", [])),
424 unmodified lines
612 http2: bool = False,613 proxies: ProxiesTypes = None,614 mounts: typing.Mapping[str, BaseTransport] = None,615 timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,616 follow_redirects: bool = False,617 limits: Limits = DEFAULT_LIMITS,618 max_redirects: int = DEFAULT_MAX_REDIRECTS,619 event_hooks: typing.Mapping[str, typing.List[typing.Callable]] = None,620 base_url: URLTypes = "",
6 unmodified lines
627 params=params,628 headers=headers,629 cookies=cookies,630 timeout=timeout,631 follow_redirects=follow_redirects,632 max_redirects=max_redirects,633 event_hooks=event_hooks,634 base_url=base_url,635 trust_env=trust_env,
111 unmodified lines
747 params: QueryParamTypes = None,748 headers: HeaderTypes = None,749 cookies: CookieTypes = None,750 auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,749 allow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,751 follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,752 timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,753 ) -> Response:754 """755 Build and send a request.
30 unmodified lines
786 headers=headers,787 cookies=cookies,788 )789 return self.send(788 request, auth=auth, allow_redirects=allow_redirects, timeout=timeout790 request, auth=auth, follow_redirects=follow_redirects, timeout=timeout791 )792793 @contextmanager794 def stream(
8 unmodified lines
803 params: QueryParamTypes = None,804 headers: HeaderTypes = None,805 cookies: CookieTypes = None,806 auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,805 allow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,807 follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,808 timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,809 ) -> typing.Iterator[Response]:810 """811 Alternative to `httpx.request()` that streams the response body
18 unmodified lines
830 )831 response = self.send(832 request=request,833 auth=auth,832 allow_redirects=allow_redirects,834 follow_redirects=follow_redirects,835 timeout=timeout,836 stream=True,837 )838 try:
6 unmodified lines
845 request: Request,846 *,847 stream: bool = False,848 auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,847 allow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,849 follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,850 timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,851 ) -> Response:852 """853 Send a request.
14 unmodified lines
868 self._state = ClientState.OPENED869 timeout = (870 self.timeout if isinstance(timeout, UseClientDefault) else Timeout(timeout)871 )870 allow_redirects = (871 self.allow_redirects872 if isinstance(allow_redirects, UseClientDefault)873 else allow_redirects872 follow_redirects = (873 self.follow_redirects874 if isinstance(follow_redirects, UseClientDefault)875 else follow_redirects876 )877878 auth = self._build_request_auth(request, auth)879880 response = self._send_handling_auth(881 request,882 auth=auth,883 timeout=timeout,882 allow_redirects=allow_redirects,884 follow_redirects=follow_redirects,885 history=[],886 )887 try:888 if not stream:
9 unmodified lines
898 self,899 request: Request,900 auth: Auth,901 timeout: Timeout,900 allow_redirects: bool,902 follow_redirects: bool,903 history: typing.List[Response],904 ) -> Response:905 auth_flow = auth.sync_auth_flow(request)906 try:
2 unmodified lines
909 while True:910 response = self._send_handling_redirects(911 request,912 timeout=timeout,911 allow_redirects=allow_redirects,913 follow_redirects=follow_redirects,914 history=history,915 )916 try:917 try:
15 unmodified lines
933 def _send_handling_redirects(934 self,935 request: Request,936 timeout: Timeout,935 allow_redirects: bool,937 follow_redirects: bool,938 history: typing.List[Response],939 ) -> Response:940 while True:941 if len(history) > self.max_redirects:
15 unmodified lines
957958 request = self._build_redirect_request(request, response)959 history = history + [response]960959 if allow_redirects:961 if follow_redirects:962 response.read()963 else:964 response.next_request = request965 return response
48 unmodified lines
1014 params: QueryParamTypes = None,1015 headers: HeaderTypes = None,1016 cookies: CookieTypes = None,1017 auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1016 allow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1018 follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1019 timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1020 ) -> Response:1021 """1022 Send a `GET` request.
6 unmodified lines
1029 params=params,1030 headers=headers,1031 cookies=cookies,1032 auth=auth,1031 allow_redirects=allow_redirects,1033 follow_redirects=follow_redirects,1034 timeout=timeout,1035 )10361037 def options(
3 unmodified lines
1041 params: QueryParamTypes = None,1042 headers: HeaderTypes = None,1043 cookies: CookieTypes = None,1044 auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1043 allow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1045 follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1046 timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1047 ) -> Response:1048 """1049 Send an `OPTIONS` request.
6 unmodified lines
1056 params=params,1057 headers=headers,1058 cookies=cookies,1059 auth=auth,1058 allow_redirects=allow_redirects,1060 follow_redirects=follow_redirects,1061 timeout=timeout,1062 )10631064 def head(
3 unmodified lines
1068 params: QueryParamTypes = None,1069 headers: HeaderTypes = None,1070 cookies: CookieTypes = None,1071 auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1070 allow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1072 follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1073 timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1074 ) -> Response:1075 """1076 Send a `HEAD` request.
6 unmodified lines
1083 params=params,1084 headers=headers,1085 cookies=cookies,1086 auth=auth,1085 allow_redirects=allow_redirects,1087 follow_redirects=follow_redirects,1088 timeout=timeout,1089 )10901091 def post(
7 unmodified lines
1099 params: QueryParamTypes = None,1100 headers: HeaderTypes = None,1101 cookies: CookieTypes = None,1102 auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1101 allow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1103 follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1104 timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1105 ) -> Response:1106 """1107 Send a `POST` request.
10 unmodified lines
1118 params=params,1119 headers=headers,1120 cookies=cookies,1121 auth=auth,1120 allow_redirects=allow_redirects,1122 follow_redirects=follow_redirects,1123 timeout=timeout,1124 )11251126 def put(
7 unmodified lines
1134 params: QueryParamTypes = None,1135 headers: HeaderTypes = None,1136 cookies: CookieTypes = None,1137 auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1136 allow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1138 follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1139 timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1140 ) -> Response:1141 """1142 Send a `PUT` request.
10 unmodified lines
1153 params=params,1154 headers=headers,1155 cookies=cookies,1156 auth=auth,1155 allow_redirects=allow_redirects,1157 follow_redirects=follow_redirects,1158 timeout=timeout,1159 )11601161 def patch(
7 unmodified lines
1169 params: QueryParamTypes = None,1170 headers: HeaderTypes = None,1171 cookies: CookieTypes = None,1172 auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1171 allow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1173 follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1174 timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1175 ) -> Response:1176 """1177 Send a `PATCH` request.
10 unmodified lines
1188 params=params,1189 headers=headers,1190 cookies=cookies,1191 auth=auth,1190 allow_redirects=allow_redirects,1192 follow_redirects=follow_redirects,1193 timeout=timeout,1194 )11951196 def delete(
3 unmodified lines
1200 params: QueryParamTypes = None,1201 headers: HeaderTypes = None,1202 cookies: CookieTypes = None,1203 auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1202 allow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1204 follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1205 timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1206 ) -> Response:1207 """1208 Send a `DELETE` request.
6 unmodified lines
1215 params=params,1216 headers=headers,1217 cookies=cookies,1218 auth=auth,1217 allow_redirects=allow_redirects,1219 follow_redirects=follow_redirects,1220 timeout=timeout,1221 )12221223 def close(self) -> None:
106 unmodified lines
1330 http2: bool = False,1331 proxies: ProxiesTypes = None,1332 mounts: typing.Mapping[str, AsyncBaseTransport] = None,1333 timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,1334 follow_redirects: bool = False,1335 limits: Limits = DEFAULT_LIMITS,1336 max_redirects: int = DEFAULT_MAX_REDIRECTS,1337 event_hooks: typing.Mapping[str, typing.List[typing.Callable]] = None,1338 base_url: URLTypes = "",
6 unmodified lines
1345 params=params,1346 headers=headers,1347 cookies=cookies,1348 timeout=timeout,1349 follow_redirects=follow_redirects,1350 max_redirects=max_redirects,1351 event_hooks=event_hooks,1352 base_url=base_url,1353 trust_env=trust_env,
110 unmodified lines
1464 params: QueryParamTypes = None,1465 headers: HeaderTypes = None,1466 cookies: CookieTypes = None,1467 auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1464 allow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1468 follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1469 timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1470 ) -> Response:1471 """1472 Build and send a request.
22 unmodified lines
1495 headers=headers,1496 cookies=cookies,1497 )1498 response = await self.send(1495 request, auth=auth, allow_redirects=allow_redirects, timeout=timeout1499 request, auth=auth, follow_redirects=follow_redirects, timeout=timeout1500 )1501 return response15021503 @asynccontextmanager
9 unmodified lines
1513 params: QueryParamTypes = None,1514 headers: HeaderTypes = None,1515 cookies: CookieTypes = None,1516 auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1513 allow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1517 follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1518 timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1519 ) -> typing.AsyncIterator[Response]:1520 """1521 Alternative to `httpx.request()` that streams the response body
18 unmodified lines
1540 )1541 response = await self.send(1542 request=request,1543 auth=auth,1540 allow_redirects=allow_redirects,1544 follow_redirects=follow_redirects,1545 timeout=timeout,1546 stream=True,1547 )1548 try:
6 unmodified lines
1555 request: Request,1556 *,1557 stream: bool = False,1558 auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1555 allow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1559 follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1560 timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1561 ) -> Response:1562 """1563 Send a request.
14 unmodified lines
1578 self._state = ClientState.OPENED1579 timeout = (1580 self.timeout if isinstance(timeout, UseClientDefault) else Timeout(timeout)1581 )1578 allow_redirects = (1579 self.allow_redirects1580 if isinstance(allow_redirects, UseClientDefault)1581 else allow_redirects1582 follow_redirects = (1583 self.follow_redirects1584 if isinstance(follow_redirects, UseClientDefault)1585 else follow_redirects1586 )15871588 auth = self._build_request_auth(request, auth)15891590 response = await self._send_handling_auth(1591 request,1592 auth=auth,1593 timeout=timeout,1590 allow_redirects=allow_redirects,1594 follow_redirects=follow_redirects,1595 history=[],1596 )1597 try:1598 if not stream:
9 unmodified lines
1608 self,1609 request: Request,1610 auth: Auth,1611 timeout: Timeout,1608 allow_redirects: bool,1612 follow_redirects: bool,1613 history: typing.List[Response],1614 ) -> Response:1615 auth_flow = auth.async_auth_flow(request)1616 try:
2 unmodified lines
1619 while True:1620 response = await self._send_handling_redirects(1621 request,1622 timeout=timeout,1619 allow_redirects=allow_redirects,1623 follow_redirects=follow_redirects,1624 history=history,1625 )1626 try:1627 try:
15 unmodified lines
1643 async def _send_handling_redirects(1644 self,1645 request: Request,1646 timeout: Timeout,1643 allow_redirects: bool,1647 follow_redirects: bool,1648 history: typing.List[Response],1649 ) -> Response:1650 while True:1651 if len(history) > self.max_redirects:
16 unmodified lines
16681669 request = self._build_redirect_request(request, response)1670 history = history + [response]16711668 if allow_redirects:1672 if follow_redirects:1673 await response.aread()1674 else:1675 response.next_request = request1676 return response
55 unmodified lines
1732 params: QueryParamTypes = None,1733 headers: HeaderTypes = None,1734 cookies: CookieTypes = None,1735 auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1732 allow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1736 follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1737 timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1738 ) -> Response:1739 """1740 Send a `GET` request.
6 unmodified lines
1747 params=params,1748 headers=headers,1749 cookies=cookies,1750 auth=auth,1747 allow_redirects=allow_redirects,1751 follow_redirects=follow_redirects,1752 timeout=timeout,1753 )17541755 async def options(
3 unmodified lines
1759 params: QueryParamTypes = None,1760 headers: HeaderTypes = None,1761 cookies: CookieTypes = None,1762 auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1759 allow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1763 follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1764 timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1765 ) -> Response:1766 """1767 Send an `OPTIONS` request.
6 unmodified lines
1774 params=params,1775 headers=headers,1776 cookies=cookies,1777 auth=auth,1774 allow_redirects=allow_redirects,1778 follow_redirects=follow_redirects,1779 timeout=timeout,1780 )17811782 async def head(
3 unmodified lines
1786 params: QueryParamTypes = None,1787 headers: HeaderTypes = None,1788 cookies: CookieTypes = None,1789 auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1786 allow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1790 follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1791 timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1792 ) -> Response:1793 """1794 Send a `HEAD` request.
6 unmodified lines
1801 params=params,1802 headers=headers,1803 cookies=cookies,1804 auth=auth,1801 allow_redirects=allow_redirects,1805 follow_redirects=follow_redirects,1806 timeout=timeout,1807 )18081809 async def post(
7 unmodified lines
1817 params: QueryParamTypes = None,1818 headers: HeaderTypes = None,1819 cookies: CookieTypes = None,1820 auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1817 allow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1821 follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1822 timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1823 ) -> Response:1824 """1825 Send a `POST` request.
10 unmodified lines
1836 params=params,1837 headers=headers,1838 cookies=cookies,1839 auth=auth,1836 allow_redirects=allow_redirects,1840 follow_redirects=follow_redirects,1841 timeout=timeout,1842 )18431844 async def put(
7 unmodified lines
1852 params: QueryParamTypes = None,1853 headers: HeaderTypes = None,1854 cookies: CookieTypes = None,1855 auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1852 allow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1856 follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1857 timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1858 ) -> Response:1859 """1860 Send a `PUT` request.
10 unmodified lines
1871 params=params,1872 headers=headers,1873 cookies=cookies,1874 auth=auth,1871 allow_redirects=allow_redirects,1875 follow_redirects=follow_redirects,1876 timeout=timeout,1877 )18781879 async def patch(
7 unmodified lines
1887 params: QueryParamTypes = None,1888 headers: HeaderTypes = None,1889 cookies: CookieTypes = None,1890 auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1887 allow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1891 follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1892 timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1893 ) -> Response:1894 """1895 Send a `PATCH` request.
10 unmodified lines
1906 params=params,1907 headers=headers,1908 cookies=cookies,1909 auth=auth,1906 allow_redirects=allow_redirects,1910 follow_redirects=follow_redirects,1911 timeout=timeout,1912 )19131914 async def delete(
3 unmodified lines
1918 params: QueryParamTypes = None,1919 headers: HeaderTypes = None,1920 cookies: CookieTypes = None,1921 auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1918 allow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1922 follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT,1923 timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1924 ) -> Response:1925 """1926 Send a `DELETE` request.
6 unmodified lines
1933 params=params,1934 headers=headers,1935 cookies=cookies,1936 auth=auth,1933 allow_redirects=allow_redirects,1937 follow_redirects=follow_redirects,1938 timeout=timeout,1939 )19401941 async def aclose(self) -> None:
70 unmodified lines
modified:httpx/_models.py+1 added-1 removed
1207 unmodified lines
1208 self.headers = Headers(headers)12091210 self._request: typing.Optional[Request] = request12111212 # When allow_redirects=False and a redirect is received,1212 # When follow_redirects=False and a redirect is received,1213 # the client will set `response.next_request`.1214 self.next_request: typing.Optional[Request] = None12151216 self.extensions = {} if extensions is None else extensions
646 unmodified lines
modified:tests/client/test_event_hooks.py+6 added-2 removed
129 unmodified lines
130131 event_hooks = {"request": [on_request], "response": [on_response]}132133 with httpx.Client(134 event_hooks=event_hooks, transport=httpx.MockTransport(app)134 event_hooks=event_hooks,135 transport=httpx.MockTransport(app),136 follow_redirects=True,137 ) as http:138 http.get("http://127.0.0.1:8000/redirect", auth=("username", "password"))139140 assert events == [
46 unmodified lines
187188 event_hooks = {"request": [on_request], "response": [on_response]}189190 async with httpx.AsyncClient(189 event_hooks=event_hooks, transport=httpx.MockTransport(app)191 event_hooks=event_hooks,192 transport=httpx.MockTransport(app),193 follow_redirects=True,194 ) as http:195 await http.get("http://127.0.0.1:8000/redirect", auth=("username", "password"))196197 assert events == [
31 unmodified lines
modified:tests/client/test_redirects.py+46 added-28 removed
111 unmodified lines
112113114def test_redirect_301():115 client = httpx.Client(transport=httpx.MockTransport(redirects))116 response = client.post("https://example.org/redirect_301")116 response = client.post("https://example.org/redirect_301", follow_redirects=True)117 assert response.status_code == httpx.codes.OK118 assert response.url == "https://example.org/"119 assert len(response.history) == 1120121122def test_redirect_302():123 client = httpx.Client(transport=httpx.MockTransport(redirects))124 response = client.post("https://example.org/redirect_302")124 response = client.post("https://example.org/redirect_302", follow_redirects=True)125 assert response.status_code == httpx.codes.OK126 assert response.url == "https://example.org/"127 assert len(response.history) == 1128129130def test_redirect_303():131 client = httpx.Client(transport=httpx.MockTransport(redirects))132 response = client.get("https://example.org/redirect_303")132 response = client.get("https://example.org/redirect_303", follow_redirects=True)133 assert response.status_code == httpx.codes.OK134 assert response.url == "https://example.org/"135 assert len(response.history) == 1136137138def test_next_request():139 client = httpx.Client(transport=httpx.MockTransport(redirects))140 request = client.build_request("POST", "https://example.org/redirect_303")141 response = client.send(request, allow_redirects=False)141 response = client.send(request, follow_redirects=False)142 assert response.status_code == httpx.codes.SEE_OTHER143 assert response.url == "https://example.org/redirect_303"144 assert response.next_request is not None145146 response = client.send(response.next_request, allow_redirects=False)146 response = client.send(response.next_request, follow_redirects=False)147 assert response.status_code == httpx.codes.OK148 assert response.url == "https://example.org/"149 assert response.next_request is None150
1 unmodified line
152@pytest.mark.usefixtures("async_environment")153async def test_async_next_request():154 async with httpx.AsyncClient(transport=httpx.MockTransport(redirects)) as client:155 request = client.build_request("POST", "https://example.org/redirect_303")156 response = await client.send(request, allow_redirects=False)156 response = await client.send(request, follow_redirects=False)157 assert response.status_code == httpx.codes.SEE_OTHER158 assert response.url == "https://example.org/redirect_303"159 assert response.next_request is not None160161 response = await client.send(response.next_request, allow_redirects=False)161 response = await client.send(response.next_request, follow_redirects=False)162 assert response.status_code == httpx.codes.OK163 assert response.url == "https://example.org/"164 assert response.next_request is None165
2 unmodified lines
168 """169 Contrary to Requests, redirects remain enabled by default for HEAD requests.170 """171 client = httpx.Client(transport=httpx.MockTransport(redirects))172 response = client.head("https://example.org/redirect_302")172 response = client.head("https://example.org/redirect_302", follow_redirects=True)173 assert response.status_code == httpx.codes.OK174 assert response.url == "https://example.org/"175 assert response.request.method == "HEAD"176 assert len(response.history) == 1
1 unmodified line
178179180def test_relative_redirect():181 client = httpx.Client(transport=httpx.MockTransport(redirects))182 response = client.get("https://example.org/relative_redirect")182 response = client.get(183 "https://example.org/relative_redirect", follow_redirects=True184 )185 assert response.status_code == httpx.codes.OK186 assert response.url == "https://example.org/"187 assert len(response.history) == 1188189190def test_malformed_redirect():191 # https://github.com/encode/httpx/issues/771192 client = httpx.Client(transport=httpx.MockTransport(redirects))191 response = client.get("http://example.org/malformed_redirect")193 response = client.get(194 "http://example.org/malformed_redirect", follow_redirects=True195 )196 assert response.status_code == httpx.codes.OK197 assert response.url == "https://example.org:443/"198 assert len(response.history) == 1199200201def test_invalid_redirect():202 client = httpx.Client(transport=httpx.MockTransport(redirects))203 with pytest.raises(httpx.RemoteProtocolError):200 client.get("http://example.org/invalid_redirect")204 client.get("http://example.org/invalid_redirect", follow_redirects=True)205206207def test_no_scheme_redirect():208 client = httpx.Client(transport=httpx.MockTransport(redirects))205 response = client.get("https://example.org/no_scheme_redirect")209 response = client.get(210 "https://example.org/no_scheme_redirect", follow_redirects=True211 )212 assert response.status_code == httpx.codes.OK213 assert response.url == "https://example.org/"214 assert len(response.history) == 1215216217def test_fragment_redirect():218 client = httpx.Client(transport=httpx.MockTransport(redirects))213 response = client.get("https://example.org/relative_redirect#fragment")219 response = client.get(220 "https://example.org/relative_redirect#fragment", follow_redirects=True221 )222 assert response.status_code == httpx.codes.OK223 assert response.url == "https://example.org/#fragment"224 assert len(response.history) == 1225226227def test_multiple_redirects():228 client = httpx.Client(transport=httpx.MockTransport(redirects))221 response = client.get("https://example.org/multiple_redirects?count=20")229 response = client.get(230 "https://example.org/multiple_redirects?count=20", follow_redirects=True231 )232 assert response.status_code == httpx.codes.OK233 assert response.url == "https://example.org/multiple_redirects"234 assert len(response.history) == 20235 assert response.history[0].url == "https://example.org/multiple_redirects?count=20"
5 unmodified lines
241@pytest.mark.usefixtures("async_environment")242async def test_async_too_many_redirects():243 async with httpx.AsyncClient(transport=httpx.MockTransport(redirects)) as client:244 with pytest.raises(httpx.TooManyRedirects):235 await client.get("https://example.org/multiple_redirects?count=21")245 await client.get(246 "https://example.org/multiple_redirects?count=21", follow_redirects=True247 )248249250def test_sync_too_many_redirects():251 client = httpx.Client(transport=httpx.MockTransport(redirects))252 with pytest.raises(httpx.TooManyRedirects):241 client.get("https://example.org/multiple_redirects?count=21")253 client.get(254 "https://example.org/multiple_redirects?count=21", follow_redirects=True255 )256257258def test_redirect_loop():259 client = httpx.Client(transport=httpx.MockTransport(redirects))260 with pytest.raises(httpx.TooManyRedirects):247 client.get("https://example.org/redirect_loop")261 client.get("https://example.org/redirect_loop", follow_redirects=True)262263264def test_cross_domain_redirect_with_auth_header():265 client = httpx.Client(transport=httpx.MockTransport(redirects))266 url = "https://example.com/cross_domain"267 headers = {"Authorization": "abc"}254 response = client.get(url, headers=headers)268 response = client.get(url, headers=headers, follow_redirects=True)269 assert response.url == "https://example.org/cross_domain_target"270 assert "authorization" not in response.json()["headers"]271272273def test_cross_domain_redirect_with_auth():274 client = httpx.Client(transport=httpx.MockTransport(redirects))275 url = "https://example.com/cross_domain"262 response = client.get(url, auth=("user", "pass"))276 response = client.get(url, auth=("user", "pass"), follow_redirects=True)277 assert response.url == "https://example.org/cross_domain_target"278 assert "authorization" not in response.json()["headers"]279280281def test_same_domain_redirect():282 client = httpx.Client(transport=httpx.MockTransport(redirects))283 url = "https://example.org/cross_domain"284 headers = {"Authorization": "abc"}271 response = client.get(url, headers=headers)285 response = client.get(url, headers=headers, follow_redirects=True)286 assert response.url == "https://example.org/cross_domain_target"287 assert response.json()["headers"]["authorization"] == "abc"288289
3 unmodified lines
293 """294 client = httpx.Client(transport=httpx.MockTransport(redirects))295 url = "https://example.org/redirect_body"296 content = b"Example request body"283 response = client.post(url, content=content)297 response = client.post(url, content=content, follow_redirects=True)298 assert response.url == "https://example.org/redirect_body_target"299 assert response.json()["body"] == "Example request body"300 assert "content-length" in response.json()["headers"]301
4 unmodified lines
306 """307 client = httpx.Client(transport=httpx.MockTransport(redirects))308 url = "https://example.org/redirect_no_body"309 content = b"Example request body"296 response = client.post(url, content=content)310 response = client.post(url, content=content, follow_redirects=True)311 assert response.url == "https://example.org/redirect_body_target"312 assert response.json()["body"] == ""313 assert "content-length" not in response.json()["headers"]314315316def test_can_stream_if_no_redirect():317 client = httpx.Client(transport=httpx.MockTransport(redirects))318 url = "https://example.org/redirect_301"305 with client.stream("GET", url, allow_redirects=False) as response:319 with client.stream("GET", url, follow_redirects=False) as response:320 assert not response.is_closed321 assert response.status_code == httpx.codes.MOVED_PERMANENTLY322 assert response.headers["location"] == "https://example.org/"323
5 unmodified lines
329 def streaming_body():330 yield b"Example request body" # pragma: nocover331332 with pytest.raises(httpx.StreamConsumed):319 client.post(url, content=streaming_body())333 client.post(url, content=streaming_body(), follow_redirects=True)334335336def test_cross_subdomain_redirect():337 client = httpx.Client(transport=httpx.MockTransport(redirects))338 url = "https://example.com/cross_subdomain"325 response = client.get(url)339 response = client.get(url, follow_redirects=True)340 assert response.url == "https://www.example.org/cross_subdomain"341342343def cookie_sessions(request: httpx.Request) -> httpx.Response:
29 unmodified lines
373 return httpx.Response(status_code, headers=headers)374375376def test_redirect_cookie_behavior():363 client = httpx.Client(transport=httpx.MockTransport(cookie_sessions))377 client = httpx.Client(378 transport=httpx.MockTransport(cookie_sessions), follow_redirects=True379 )380381 # The client is not logged in.382 response = client.get("https://example.com/")383 assert response.url == "https://example.com/"
22 unmodified lines
406407def test_redirect_custom_scheme():408 client = httpx.Client(transport=httpx.MockTransport(redirects))409 with pytest.raises(httpx.UnsupportedProtocol) as e:394 client.post("https://example.org/redirect_custom_scheme")410 client.post("https://example.org/redirect_custom_scheme", follow_redirects=True)411 assert str(e.value) == "Scheme 'market' not supported."412413414@pytest.mark.usefixtures("async_environment")415async def test_async_invalid_redirect():416 async with httpx.AsyncClient(transport=httpx.MockTransport(redirects)) as client:417 with pytest.raises(httpx.RemoteProtocolError):402 await client.get("http://example.org/invalid_redirect")418 await client.get(419 "http://example.org/invalid_redirect", follow_redirects=True420 )
modified:tests/test_utils.py+1 added-1 removed
118 unmodified lines
119120@pytest.mark.asyncio121async def test_logs_redirect_chain(server, capsys):122 with override_log_level("debug"):123 async with httpx.AsyncClient() as client:123 async with httpx.AsyncClient(follow_redirects=True) as client:124 response = await client.get(server.url.copy_with(path="/redirect_301"))125 assert response.status_code == 200126127 stderr = capsys.readouterr().err.strip()
132 unmodified lines