diff --git a/httpx/_content.py b/httpx/_content.py index 6f479a0885..2579fd39e3 100644 --- a/httpx/_content.py +++ b/httpx/_content.py @@ -208,7 +208,7 @@ def encode_request( if content is not None: return encode_content(content) - elif files: + elif files is not None: return encode_multipart_data(data or {}, files, boundary) elif data: return encode_urlencoded_data(data) diff --git a/httpx/_main.py b/httpx/_main.py index cffa4bb7db..52fbecd3f4 100644 --- a/httpx/_main.py +++ b/httpx/_main.py @@ -483,7 +483,7 @@ def main( params=list(params), content=content, data=dict(data), - files=files, # type: ignore + files=files or None, # type: ignore json=json, headers=headers, cookies=dict(cookies), diff --git a/tests/test_content.py b/tests/test_content.py index f63ec18a6b..6c968a3feb 100644 --- a/tests/test_content.py +++ b/tests/test_content.py @@ -344,7 +344,7 @@ async def test_multipart_data_and_files_content(): @pytest.mark.anyio async def test_empty_request(): - request = httpx.Request(method, url, data={}, files={}) + request = httpx.Request(method, url, data={}) assert isinstance(request.stream, typing.Iterable) assert isinstance(request.stream, typing.AsyncIterable) @@ -516,3 +516,13 @@ def test_allow_nan_false(): ValueError, match="Out of range float values are not JSON compliant" ): httpx.Response(200, json=data_with_inf) + + +def test_encode_request_with_data_and_empty_files(): + request = httpx.Request( + url="https://www.example.com", method="POST", data={"key": "value"}, files={} + ) + assert request.headers["Content-Type"].startswith("multipart/form-data; boundary=") + request.read() + assert b'Content-Disposition: form-data; name="key"' in request.content + assert b"value" in request.content