I am trying to upload several files and some metadata through a POST to a server API using python and requests. I want to use the multipart/form-data type and not encode the data. I've followed the steps according to tutorials and the requests documentation, but run into several issues:
import requests
data = {
"key1": "value1",
"key2": "value2"
}
files = {
"image": open('image.jpg', 'rb')
}
headers = {
"accept": "*/*",
"Authorization": "Bearer " + token
"Content-Type": "multipart/form-data; boundary=----WebKitFormBoundaryzqgQ4h5D6Zi8"
}
r = requests.post(url, files=files, data=data, headers=headers, verify=False)
print(r.status_code, r.text)
This gets me a 400 and the error:
"Failed to read the request form. Multipart body length limit 16384 exceeded."
I read that what might help is to leave the Content-Type to requests and it will figure it out. Without the Content-Type declaration I still get a 400 and a the text back:
file is required
Now this might be a server-side issue (which I have no see over), since it demands multipart/form-data, but I suppose requests deals with the correct identification?
So far I'm out of luck, does anyone have an idea how to solve this?
EDIT: still don't know what exactly causes the issue, but found a workaround:
- added all the data directly to the URL string
- eliminated all the headers but the necessary Authorization token
It seems to me that requests is able to do a lot of the heavy lifting and too much declarations get in the way (?).