Inject generated boundary into multipart/form-data Content-Type header#1044
Open
mcmxcdev wants to merge 2 commits into
Open
Inject generated boundary into multipart/form-data Content-Type header#1044mcmxcdev wants to merge 2 commits into
mcmxcdev wants to merge 2 commits into
Conversation
When a user supplies a `multipart/form-data` Content-Type header without an explicit `boundary` parameter, httpx generated a boundary for the request body but left it out of the header. Servers then rejected the request because the header boundary did not match (or was absent from) the body. Now the generated boundary is appended to the user-supplied content-type so the header matches the body. Fixes pydantic#885 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Merging this PR will not alter performance
Comparing Footnotes
|
RFC 2046 section 5.1.1 permits boundaries containing characters such as ":" that are RFC 2045 tspecials. Such values are not tokens and must be sent as a quoted-string in the Content-Type header. Quote the boundary when needed when injecting it into the header. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #885
Problem
When a user supplies a
multipart/form-dataContent-Typeheader without an explicitboundaryparameter, httpx generated a boundary for the request body but left it out of the header. The body contained correct--<boundary>delimiters while the header read e.g.multipart/form-datawith no boundary. Servers then rejected the request (400, missing form params).Fix
When the user-supplied content-type is
multipart/form-datawith no boundary, the generated boundary is now appended to the header so it matches the boundary used in the body.is_multipart_form_data_content_type()— case-insensitive media-type check acceptingstr/bytes/None.append_boundary_to_content_type()— appends; boundary=..., normalizing trailing;/whitespace.Request.__init__injects the generated boundary into the header when none was supplied.Tests
Extended
test_multipart_header_without_boundaryto assert the header now carries a 32-char hex boundary matching the body delimiters, plus unit tests for the two new helpers. All 50 tests intest_multipart.pypass.🤖 Generated with Claude Code