I’m having trouble with a file upload to an API. My cURL command works fine but the Python requests version doesn’t. Here’s what I’ve tried:
# Working cURL command
curl -X POST "http://example.com/api/upload" \
-H "Auth: MyToken" \
-H "Accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "metadata={\"info\": [{\"type\": \"file_data\",\"details\": {\"name\": \"doc.txt\",\"desc\": \"document\"}}]}" \
-F file=@"doc.txt"
# Failing Python code
import requests
import json
url = "http://example.com/api/upload"
headers = {
"Content-Type": "multipart/form-data",
"Accept": 'application/json',
"Auth": "MyToken"
}
data = {
"info": [
{
"type": "file_data",
"details": {
"name": "doc.txt",
"desc": "document"
}
}
]
}
files = {
'metadata': (None, json.dumps(data)),
'file': ("doc.txt", open('doc.txt', 'rb'))
}
response = requests.post(url, files=files, headers=headers)
The Python code returns a 400 error. I’ve checked the request bodies and they seem the same. Any ideas what might be wrong?
I encountered a similar issue recently. The problem likely stems from how requests handles multipart/form-data. Try removing the ‘Content-Type’ header entirely and let requests handle it automatically. Also, ensure your ‘metadata’ is properly JSON-encoded.
Here’s a modified version that should work:
import requests
import json
url = "http://example.com/api/upload"
headers = {
"Accept": 'application/json',
"Auth": "MyToken"
}
metadata = json.dumps({"info": [{"type": "file_data", "details": {"name": "doc.txt", "desc": "document"}}]})
files = {
'metadata': ('metadata.json', metadata, 'application/json'),
'file': ("doc.txt", open('doc.txt', 'rb'))
}
response = requests.post(url, files=files, headers=headers)
This approach should resolve the 400 error you’re experiencing. Let me know if it helps!
I experienced a similar issue and found that the problem was linked to manually setting the Content-Type header in the Python requests library. Removing the explicit ‘Content-Type’ header lets requests automatically configure the multipart/form-data boundary, which is crucial for the API to parse the request properly.
Additionally, ensuring that the metadata is correctly encoded as JSON by specifying its MIME type in the files tuple can help. If the problem persists, consider using the requests-toolbelt for extended control over multipart uploads.
hey there! i ran into this before. try using the requests_toolbelt library instead. it gives you more control over multipart uploads. heres a quick example:
from requests_toolbelt import MultipartEncoder
import requests
m = MultipartEncoder(fields={'file': ('doc.txt', open('doc.txt', 'rb')),
'metadata': json.dumps(data)})
response = requests.post(url, data=m, headers={'Content-Type': m.content_type, 'Auth': 'MyToken'})
this shud fix ur issue. lmk if u need more help!