Skip to content

Allow multipart requests with empty files mapping or sequence - #3877

Open
meat-daemon-404 wants to merge 1 commit into
encode:masterfrom
meat-daemon-404:feat/multipart-without-files-3396
Open

meat-daemon-404 wants to merge 1 commit into
encode:masterfrom
meat-daemon-404:feat/multipart-without-files-3396

Conversation

@meat-daemon-404

Copy link
Copy Markdown

Summary

encode_request() previously checked elif files:, which meant passing an empty mapping (files={}) or sequence (files=[]) with data={...} fell through to application/x-www-form-urlencoded instead of encoding a multipart/form-data body.

Changes

  • Change elif files: to elif files is not None: in httpx/_content.py (encode_request).
  • Add test_multipart_without_files in tests/test_multipart.py covering both files={} and files=[].

Verification

  • Ran pytest --noconftest tests/test_multipart.py: all 38 tests pass.
  • Verified httpx.Request("POST", ..., data={"text": "abc"}, files={}) and files=[] produce multipart/form-data while files=None continues to produce application/x-www-form-urlencoded.

Fixes #3396

Check `files is not None` instead of truthiness in `encode_request()`
so callers can send `multipart/form-data` requests containing only
form fields by passing `files={}` or `files=[]`.

Fixes encode#3396

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Allow multipart requests without attaching files

1 participant