Skip to content

Inject generated boundary into multipart/form-data Content-Type header#1044

Open
mcmxcdev wants to merge 2 commits into
pydantic:mainfrom
mcmxcdev:fix/multipart-boundary-header-885
Open

Inject generated boundary into multipart/form-data Content-Type header#1044
mcmxcdev wants to merge 2 commits into
pydantic:mainfrom
mcmxcdev:fix/multipart-boundary-header-885

Conversation

@mcmxcdev

@mcmxcdev mcmxcdev commented Jun 24, 2026

Copy link
Copy Markdown

Fixes #885

Problem

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. The body contained correct --<boundary> delimiters while the header read e.g. multipart/form-data with no boundary. Servers then rejected the request (400, missing form params).

Fix

When the user-supplied content-type is multipart/form-data with 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 accepting str/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_boundary to 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 in test_multipart.py pass.

🤖 Generated with Claude Code

Review in cubic

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>
@codspeed-hq

codspeed-hq Bot commented Jun 24, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 15 untouched benchmarks
⏩ 7 skipped benchmarks1


Comparing mcmxcdev:fix/multipart-boundary-header-885 (100fb71) with main (82b9e2d)

Open in CodSpeed

Footnotes

  1. 7 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 3 files

Re-trigger cubic

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: multipart/formdata boundary is not sent in header

1 participant