When building Python applications that integrate with web services, you may need to send multipart form data, which contains both file uploads and regular form data. Python's standard urllib library provides the tools to handle this.
What is Multipart Form Data?
Multipart form data is used in web forms that have file uploads. Along with the file data, the form also sends regular form data like text fields. The form data is sent in multiple "parts" - one part for the file data, another part for each text field.
The web server reconstructs the parts on the receiving end. This allows files and data to be transmitted together in one request.
Creating a Multipart Form Encoder
Python's
import urllib
m = urllib.MultipartEncoder(
fields={
# regular form fields
"text_field": "text value",
"other_field": "other value",
# the upload file
"file_upload": ("filename", open("file.txt", "rb"), "text/plain")
}
)
The
Setting the Request Content-Type
When sending multipart form data, the request must include the
import requests
url = "https://api.example.com/upload"
r = requests.post(url,
data=m,
headers={"Content-Type": m.content_type})
The server uses this boundary to distinguish between each part of the multipart data.
Posting Multipart Forms with Requests
The
import requests
url = "https://api.example.com/upload"
r = requests.post(url, data=m)
Handling Large Uploads
For large file uploads, multipart encoding can consume a lot of memory. An alternative is to send the file data in chunks:
with open("big_file.dat") as f:
while True:
chunk = f.read(1024)
if not chunk:
break
requests.post(url, data=chunk)
This streams the upload directly from disk rather than loading the entire file into memory.
Key Takeaways
The