Deprecate per-request cookies (#1574)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Deprecate per-request cookies * Update docs/compatibility.md Co-authored-by: Stephen Brown II <Stephen.Brown2@gmail.com> * Update httpx/_client.py Co-authored-by: Stephen Brown II <Stephen.Brown2@gmail.com> * Update compatibility.md
authored by Tom Christie · committed by GitHub · parent 966550b · Browse files · 4 files·+64 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Stephen Brown II <Stephen.Brown2@gmail.com>
Diff stats
4 files+64 added−10 removed
- tests:+29 added−6 removed
- other:+35 added−4 removed
Provenance
- Commit
6a99f6f- Tree
19c4796- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
966550b
4 files changed+64 added−10 removed
modified:docs/compatibility.md+20 added
35 unmodified lines36373839404142434445464748495051525354555657585960616263109 unmodified linesExpand all35 unmodified linesHTTPX uses `utf-8` for encoding `str` request bodies. For example, when using `content=<str>` the request body will be encoded to `utf-8` before being sent over the wire. This differs from Requests which uses `latin1`. If you need an explicit encoding, pass encoded bytes explictly, e.g. `content=<str>.encode("latin1")`.
For response bodies, assuming the server didn't send an explicit encoding then HTTPX will do its best to figure out an appropriate encoding. Unlike Requests which uses the `chardet` library, HTTPX relies on a plainer fallback strategy (basically attempting UTF-8, or using Windows-1252 as a fallback). This strategy should be robust enough to handle the vast majority of use cases.
## Cookies
If using a client instance, then cookies should always be set on the client rather than on a per-request basis.
This usage is supported:
```pythonclient = httpx.Client(cookies=...)client.post(...)```
This usage is **not** supported:
```pythonclient = httpx.Client()client.post(..., cookies=...)```
We prefer enforcing a stricter API here because it provides clearer expectations around cookie persistence, particularly when redirects occur.
## Status Codes
In our documentation we prefer the uppercased versions, such as `codes.NOT_FOUND`, but also provide lower-cased versions for API compatibility with `requests`.
109 unmodified linesExpand allmodified:httpx/_api.py+7 added-4 removed
88 unmodified lines89 <Response [200 OK]>90 ```91 """92 with Client(93- proxies=proxies, cert=cert, verify=verify, timeout=timeout, trust_env=trust_env93+ cookies=cookies,94+ proxies=proxies,95+ cert=cert,96+ verify=verify,97+ timeout=timeout,98+ trust_env=trust_env,99 ) as client:100 return client.request(101 method=method,102 url=url,2 unmodified lines105 files=files,106 json=json,107 params=params,108 headers=headers,104- cookies=cookies,109 auth=auth,110 allow_redirects=allow_redirects,111 )112 28 unmodified lines141 142 [0]: /quickstart#streaming-responses143 """144 with Client(141- proxies=proxies, cert=cert, verify=verify, trust_env=trust_env145+ cookies=cookies, proxies=proxies, cert=cert, verify=verify, trust_env=trust_env146 ) as client:147 with client.stream(148 method=method,149 url=url,2 unmodified lines152 files=files,153 json=json,154 params=params,155 headers=headers,152- cookies=cookies,156 auth=auth,157 allow_redirects=allow_redirects,158 ) as response:159 yield response281 unmodified linesmodified:httpx/_client.py+8 added
733 unmodified lines734 are merged with client-level configuration.735 736 [0]: /advanced/#merging-of-configuration737 """738+ if cookies is not None:739+ message = (740+ "Setting per-request cookies=<...> is being deprecated, because "741+ "the expected behaviour on cookie persistence is ambiguous. Set "742+ "cookies directly on the client instance instead."743+ )744+ warnings.warn(message, DeprecationWarning)745+746 request = self.build_request(747 method=method,748 url=url,749 content=content,1187 unmodified linesmodified:tests/client/test_cookies.py+29 added-6 removed
1 from http.cookiejar import Cookie, CookieJar2 3+import pytest4+5 import httpx6 7 8 def get_and_set_cookies(request: httpx.Request) -> httpx.Response:12 unmodified lines21 """22 url = "http://example.org/echo_cookies"23 cookies = {"example-name": "example-value"}24 25+ client = httpx.Client(26+ cookies=cookies, transport=httpx.MockTransport(get_and_set_cookies)27+ )28+ response = client.get(url)29+30+ assert response.status_code == 20031+ assert response.json() == {"cookies": "example-name=example-value"}32+33+34+def test_set_per_request_cookie_is_deprecated() -> None:35+ """36+ Sending a request including a per-request cookie is deprecated.37+ """38+ url = "http://example.org/echo_cookies"39+ cookies = {"example-name": "example-value"}40+41 client = httpx.Client(transport=httpx.MockTransport(get_and_set_cookies))42+ with pytest.warns(DeprecationWarning):24- response = client.get(url, cookies=cookies)43+ response = client.get(url, cookies=cookies)44 45 assert response.status_code == 20046 assert response.json() == {"cookies": "example-name=example-value"}47 25 unmodified lines73 rfc2109=False,74 )75 cookies.set_cookie(cookie)76 58- client = httpx.Client(transport=httpx.MockTransport(get_and_set_cookies))59- response = client.get(url, cookies=cookies)77+ client = httpx.Client(78+ cookies=cookies, transport=httpx.MockTransport(get_and_set_cookies)79+ )80+ response = client.get(url)81 82 assert response.status_code == 20083 assert response.json() == {"cookies": "example-name=example-value"}84 25 unmodified lines110 rfc2109=False,111 )112 cookies.set_cookie(cookie)113 93- client = httpx.Client(transport=httpx.MockTransport(get_and_set_cookies))94- client.cookies = cookies # type: ignore114+ client = httpx.Client(115+ cookies=cookies, transport=httpx.MockTransport(get_and_set_cookies)116+ )117 response = client.get(url)118 119 assert response.status_code == 200120 assert response.json() == {"cookies": "example-name=example-value"}8 unmodified lines129 cookies = httpx.Cookies()130 cookies["example-name"] = "example-value"131 132 client = httpx.Client(transport=httpx.MockTransport(get_and_set_cookies))133+ client.cookies = cookies111- response = client.get(url, cookies=cookies)134+ response = client.get(url)135 136 assert response.status_code == 200137 assert response.json() == {"cookies": "example-name=example-value"}138 30 unmodified lines