Escalate the distinction between data=... and content=... to be stricter (#1573)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tom Christie · committed by GitHub · parent 073a328 · Browse files · 5 files·+16 added−10 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+16 added−10 removed
- tests:+10 added−7 removed
- other:+6 added−3 removed
Provenance
- Commit
397aad9- Tree
5675cd8- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
073a328
5 files changed+16 added−10 removed
modified:docs/compatibility.md+3 added-3 removed
26 unmodified lines272829303132313233343536109 unmodified linesExpand all14614714814915015015115226 unmodified lines# Uploading form data.httpx.post(..., data={"message": "Hello, world"})```
If you're using a type checking tool such as `mypy`, you'll see warnings issues if using test/byte content with the `data` argument.However, for compatibility reasons with `requests`, we do still handle the case where `data=...` is used with raw binary and text contents.Using the `data=<text/byte content>` will raise a deprecation warning,and is expected to be fully removed with the HTTPX 1.0 release.
## Content encoding
HTTPX 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")`.109 unmodified linesExpand all## Event Hooks
`requests` allows event hooks to mutate `Request` and `Response` objects. See [examples](https://requests.readthedocs.io/en/master/user/advanced/#event-hooks) given in the documentation for `requests`.
In HTTPX, event hooks may access properties of requests and responses, but event hook callbacks cannot mutate the original request/response. In HTTPX, event hooks may access properties of requests and responses, but event hook callbacks cannot mutate the original request/response.
If you are looking for more control, consider checking out [Custom Transports](advanced.md#custom-transports).modified:httpx/_content.py+3 added
1 import inspect2+import warnings3 from json import dumps as json_dumps4 from typing import (5 Any,6 AsyncIterable,141 unmodified lines148 #149 # However for compat with requests, we *do* still support150 # `data=<bytes...>` usages. We deal with that case here, treating it151 # as if `content=<...>` had been supplied instead.152+ message = "Use 'content=<...>' to upload raw bytes/text content."153+ warnings.warn(message, DeprecationWarning)154 return encode_content(data)155 156 if content is not None:157 return encode_content(content)30 unmodified linesmodified:tests/client/test_auth.py+1 added-1 removed
628 unmodified lines629 yield b"Example request body" # pragma: nocover630 631 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:632 with pytest.raises(httpx.StreamConsumed):633- await client.post(url, data=streaming_body(), auth=auth)633+ await client.post(url, content=streaming_body(), auth=auth)634 635 636 @pytest.mark.asyncio637 async def test_async_auth_reads_response_body() -> None:62 unmodified linesmodified:tests/models/test_requests.py+3 added-3 removed
110 unmodified lines111 yield data # pragma: nocover112 113 data = streaming_body(b"test 123")114 115- request = httpx.Request("POST", "http://example.org", data=data)115+ request = httpx.Request("POST", "http://example.org", content=data)116 assert "Content-Length" not in request.headers117 assert request.headers["Transfer-Encoding"] == "chunked"118 119 8 unmodified lines128 129 data = streaming_body(b"abcd")130 131 headers = {"Content-Length": "4"}132- request = httpx.Request("POST", "http://example.org", data=data, headers=headers)132+ request = httpx.Request("POST", "http://example.org", content=data, headers=headers)133 assert "Transfer-Encoding" not in request.headers134 assert request.headers["Content-Length"] == "4"135 136 17 unmodified lines154 155 data = streaming_body(b"test 123")156 headers = {"Content-Length": "8"}157 158- request = httpx.Request("POST", "http://example.org", data=data, headers=headers)158+ request = httpx.Request("POST", "http://example.org", content=data, headers=headers)159 assert request.headers["Content-Length"] == "8"160 161 162 def test_url():13 unmodified linesmodified:tests/test_content.py+6 added-3 removed
33 unmodified lines34 assert sync_content == b"Hello, world!"35 assert async_content == b"Hello, world!"36 37 # Support 'data' for compat with requests.38+ with pytest.warns(DeprecationWarning):38- headers, stream = encode_request(data=b"Hello, world!") # type: ignore39+ headers, stream = encode_request(data=b"Hello, world!") # type: ignore40 assert isinstance(stream, typing.Iterable)41 assert isinstance(stream, typing.AsyncIterable)42 43 sync_content = b"".join([part for part in stream])22 unmodified lines66 with pytest.raises(httpx.StreamConsumed):67 [part for part in stream]68 69 # Support 'data' for compat with requests.70+ with pytest.warns(DeprecationWarning):69- headers, stream = encode_request(data=hello_world()) # type: ignore71+ headers, stream = encode_request(data=hello_world()) # type: ignore72 assert isinstance(stream, typing.Iterable)73 assert not isinstance(stream, typing.AsyncIterable)74 75 content = b"".join([part for part in stream])20 unmodified lines96 with pytest.raises(httpx.StreamConsumed):97 [part async for part in stream]98 99 # Support 'data' for compat with requests.100+ with pytest.warns(DeprecationWarning):98- headers, stream = encode_request(data=hello_world()) # type: ignore101+ headers, stream = encode_request(data=hello_world()) # type: ignore102 assert not isinstance(stream, typing.Iterable)103 assert isinstance(stream, typing.AsyncIterable)104 105 content = b"".join([part async for part in stream])308 unmodified lines