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
`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).
import inspect
+import warnings
from json import dumps as json_dumps
from typing import (
Any,
# However for compat with requests, we *do* still support
# `data=<bytes...>` usages. We deal with that case here, treating it
# as if `content=<...>` had been supplied instead.
+ message = "Use 'content=<...>' to upload raw bytes/text content."
+ warnings.warn(message, DeprecationWarning)
return encode_content(data)
if content is not None:
async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:
with pytest.raises(httpx.StreamConsumed):
- await client.post(url, data=streaming_body(), auth=auth)
+ await client.post(url, content=streaming_body(), auth=auth)
@pytest.mark.asyncio
data = streaming_body(b"test 123")
- request = httpx.Request("POST", "http://example.org", data=data)
+ request = httpx.Request("POST", "http://example.org", content=data)
assert "Content-Length" not in request.headers
assert request.headers["Transfer-Encoding"] == "chunked"
data = streaming_body(b"abcd")
headers = {"Content-Length": "4"}
- request = httpx.Request("POST", "http://example.org", data=data, headers=headers)
+ request = httpx.Request("POST", "http://example.org", content=data, headers=headers)
assert "Transfer-Encoding" not in request.headers
assert request.headers["Content-Length"] == "4"
data = streaming_body(b"test 123")
headers = {"Content-Length": "8"}
- request = httpx.Request("POST", "http://example.org", data=data, headers=headers)
+ request = httpx.Request("POST", "http://example.org", content=data, headers=headers)
assert request.headers["Content-Length"] == "8"
assert async_content == b"Hello, world!"
# Support 'data' for compat with requests.
- headers, stream = encode_request(data=b"Hello, world!") # type: ignore
+ with pytest.warns(DeprecationWarning):
+ headers, stream = encode_request(data=b"Hello, world!") # type: ignore
assert isinstance(stream, typing.Iterable)
assert isinstance(stream, typing.AsyncIterable)
[part for part in stream]
# Support 'data' for compat with requests.
- headers, stream = encode_request(data=hello_world()) # type: ignore
+ with pytest.warns(DeprecationWarning):
+ headers, stream = encode_request(data=hello_world()) # type: ignore
assert isinstance(stream, typing.Iterable)
assert not isinstance(stream, typing.AsyncIterable)
[part async for part in stream]
# Support 'data' for compat with requests.
- headers, stream = encode_request(data=hello_world()) # type: ignore
+ with pytest.warns(DeprecationWarning):
+ headers, stream = encode_request(data=hello_world()) # type: ignore
assert not isinstance(stream, typing.Iterable)
assert isinstance(stream, typing.AsyncIterable)