Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ what gets sent over the wire.*
* `def __init__(method, url, [params], [headers], [cookies], [content], [data], [files], [json], [stream])`
* `.method` - **str**
* `.url` - **URL**
* `.content` - **byte**, **byte iterator**, or **byte async iterator**
* `.content` - **byte**, **bytearray**, **byte iterator**, or **byte async iterator**
* `.headers` - **Headers**
* `.cookies` - **Cookies**

Expand Down
4 changes: 2 additions & 2 deletions httpx/_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ async def __aiter__(self) -> AsyncIterator[bytes]:


def encode_content(
content: str | bytes | Iterable[bytes] | AsyncIterable[bytes],
content: str | bytes | bytearray | Iterable[bytes] | AsyncIterable[bytes],
) -> tuple[dict[str, str], SyncByteStream | AsyncByteStream]:
if isinstance(content, (bytes, str)):
if isinstance(content, (bytes, bytearray, str)):
body = content.encode("utf-8") if isinstance(content, str) else content
content_length = len(body)
headers = {"Content-Length": str(content_length)} if body else {}
Expand Down
2 changes: 1 addition & 1 deletion httpx/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"Auth",
]

RequestContent = Union[str, bytes, Iterable[bytes], AsyncIterable[bytes]]
RequestContent = Union[str, bytes, bytearray, Iterable[bytes], AsyncIterable[bytes]]
ResponseContent = Union[str, bytes, Iterable[bytes], AsyncIterable[bytes]]
ResponseExtensions = Mapping[str, Any]

Expand Down
1 change: 1 addition & 0 deletions test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# TLS secrets log file, generated by OpenSSL / Python
24 changes: 24 additions & 0 deletions tests/test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,30 @@ async def test_bytes_content():
assert async_content == b"Hello, world!"


@pytest.mark.anyio
async def test_bytearray_content():
request = httpx.Request(method, url, content=b"Hello, world!")
assert isinstance(request.stream, typing.Iterable)
assert isinstance(request.stream, typing.AsyncIterable)

sync_content = b"".join(list(request.stream))
async_content = b"".join([part async for part in request.stream])

assert request.headers == {"Host": "www.example.com", "Content-Length": "13"}
assert sync_content == b"Hello, world!"
assert async_content == b"Hello, world!"

assert isinstance(request.stream, typing.Iterable)
assert isinstance(request.stream, typing.AsyncIterable)

sync_content = b"".join(list(request.stream))
async_content = b"".join([part async for part in request.stream])

assert request.headers == {"Host": "www.example.com", "Content-Length": "13"}
assert sync_content == b"Hello, world!"
assert async_content == b"Hello, world!"


@pytest.mark.anyio
async def test_bytesio_content():
request = httpx.Request(method, url, content=io.BytesIO(b"Hello, world!"))
Expand Down