Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion httpx/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ def main(
params=list(params),
content=content,
data=dict(data),
files=files, # type: ignore
files=files or None, # type: ignore
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this change is correct. It seems to go against the spirit of your main change in the httpx/_content.py file.
It seems to prevent the fix from working when using the cli.
Although I am not familiar with this usage and I didn't look too much into it, so I don't know if it's even possible to do this through the cli.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review, I'll take a look at the CLI.

json=json,
headers=headers,
cookies=dict(cookies),
Expand Down
11 changes: 10 additions & 1 deletion tests/test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import httpx


Comment thread
lovelydinosaur marked this conversation as resolved.
Outdated
method = "POST"
url = "https://www.example.com"

Expand Down Expand Up @@ -344,7 +345,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)

Expand Down Expand Up @@ -516,3 +517,11 @@ 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(data={"key": "value"}, files={})
Comment thread
lovelydinosaur marked this conversation as resolved.
Outdated
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