How to send multipart/form-data with requests in Python
Codemia Knowledge Hub ·
Pass files through the files argument of requests.post and plain text fields through data. requests then builds the multipart/form-data body and generates the boundary automatically. Most problems come from the wrong payload shape, leaked file handles, or mixing JSON with multipart.
Upload a single file
requests sets the multipart content type for you.
Upload multiple files in one request
Use a list of tuples when the same field name repeats:
Close each file handle, or open each file in a context manager.
Upload raw bytes without a file on disk
You can upload bytes generated in memory, which is useful for generated reports or transformed content:
Add authentication headers
Keep auth headers separate from the multipart construction:
Do not set Content-Type to multipart with a custom boundary yourself unless you also build the body yourself. Overriding it incorrectly breaks the upload.
Handle errors and timeouts
Set a timeout and catch transport failures:
For critical workflows, add retries with backoff for transient errors.
Send a file with JSON metadata
Multipart requests carry text fields as form values, not as a JSON body, so you cannot combine a JSON body and a multipart body in one payload. Serialize the metadata into a form field instead:
The server can then parse the meta field as a JSON string.
Reuse a session for repeated uploads
A requests.Session reduces connection overhead across repeated multipart uploads:
Large files
For very large files, watch memory use and network timeouts. If a simple multipart call cannot meet production limits, consider chunked or resumable upload APIs.
When the server rejects the upload
Most failures are contract mismatches, not transport issues. Check:
- the file field key the server expects, such as
fileorattachments - that metadata field names exactly match the backend parser
- that you sent an opened binary file, not a file path string
- that text fields are in
dataalongsidefiles
Read the API docs and inspect the server's error payload. Logging request IDs helps trace failed uploads.
Related reading
- How to send a POST request with BODY in swift
- How to send a string via PostMessage?
- How to send an HTTPS GET Request in C
- How to send email in java using asynchronous API
- How to send an email with Gmail as provider using Python?
- How to send an email with Python?
- How to send HTTP request in Java?
- How to send MultiPart Request without calling drain?
System design interview problems on this topic
- Design a Load Balancer: solution and walkthrough
- Design a Domain Name System: solution and walkthrough
- Design a Global Content Distribution Network: solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.