api file_upload ai_generated true

400 Bad Request — multipart boundary in Content-Type does not match body

ID: api/multipart-boundary-content-type-mismatch

Also available as: JSON · Markdown
90%Fix Rate
88%Confidence
4Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
any active

Root Cause

Manually setting Content-Type with wrong boundary causes multipart parse failure.

generic

Workarounds

  1. 95% success Let the HTTP library set Content-Type automatically for multipart
    requests.post(url, files={'file': open('f.txt','rb')})

    Sources: https://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file

  2. 85% success If manual boundary is needed, ensure exact match between header and body
    boundary='myboundary'; headers={'Content-Type': f'multipart/form-data; boundary={boundary}'}

    Sources: https://www.rfc-editor.org/rfc/rfc2046#section-5.1

Dead Ends

Common approaches that don't work:

  1. Manually set Content-Type: multipart/form-data header 92% fail

    When you set Content-Type manually, the boundary will not match what the HTTP library generates

  2. Copy boundary from a previous request 85% fail

    Each multipart request generates a unique boundary; reusing old ones causes mismatch