Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(content): allow multipart requests without files. #3397

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
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

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
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


tomchristie marked this conversation as resolved.
Show resolved Hide resolved
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={})
tomchristie marked this conversation as resolved.
Show resolved Hide resolved
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
Loading