Skip to content

Commit 430285f

Browse files
Fix multipart edge cases with data={} and/or files={} (#861)
* Add reproducible test example for empty multipart * Possible fix for empty combination of files/data * Return bytestream with empty data/files * Remove content-length in test Co-authored-by: florimondmanca <florimond.manca@gmail.com>
1 parent 43ec09c commit 430285f

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

httpx/_content_streams.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,15 +323,15 @@ def encode(
323323
Handles encoding the given `data`, `files`, and `json`, returning
324324
a `ContentStream` implementation.
325325
"""
326-
if data is None:
326+
if not data:
327327
if json is not None:
328328
return JSONStream(json=json)
329329
elif files:
330330
return MultipartStream(data={}, files=files, boundary=boundary)
331331
else:
332332
return ByteStream(body=b"")
333333
elif isinstance(data, dict):
334-
if files is not None:
334+
if files:
335335
return MultipartStream(data=data, files=files, boundary=boundary)
336336
else:
337337
return URLEncodedStream(data=data)

tests/test_content_streams.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,18 @@ async def test_multipart_data_and_files_content():
189189
)
190190

191191

192+
@pytest.mark.asyncio
193+
async def test_empty_request():
194+
stream = encode(data={}, files={})
195+
sync_content = b"".join([part for part in stream])
196+
async_content = b"".join([part async for part in stream])
197+
198+
assert stream.can_replay()
199+
assert stream.get_headers() == {}
200+
assert sync_content == b""
201+
assert async_content == b""
202+
203+
192204
def test_invalid_argument():
193205
with pytest.raises(TypeError):
194206
encode(123)

0 commit comments

Comments
 (0)