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 httpx/_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions tests/test_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ def test_multipart(value, output):
)


@pytest.mark.parametrize("files", ({}, []))
def test_multipart_without_files(files):
client = httpx.Client(transport=httpx.MockTransport(echo_request_content))

data = {"text": "abc"}
response = client.post("http://127.0.0.1:8000/", data=data, files=files)
boundary = response.request.headers["Content-Type"].split("boundary=")[-1]
boundary_bytes = boundary.encode("ascii")

assert response.status_code == 200
assert response.request.headers["Content-Type"].startswith(
"multipart/form-data; boundary="
)
assert response.content == b"".join(
[
b"--" + boundary_bytes + b"\r\n",
b'Content-Disposition: form-data; name="text"\r\n',
b"\r\n",
b"abc\r\n",
b"--" + boundary_bytes + b"--\r\n",
]
)


@pytest.mark.parametrize(
"header",
[
Expand Down
Loading