multipart data POST using python requests: no multipart boundary was found
Solution 1
You should NEVER set that header yourself. We set the header properly with the boundary. If you set that header, we won't and your server won't know what boundary to expect (since it is added to the header). Remove your custom Content-Type header and you'll be fine.
Solution 2
Taking out the Content-Type header with explicit "multipart/form-data" worked!
Solution 3
To specifically add boundary add following in header :
headers = {
'content-type': 'multipart/form-data; boundary=ebf9f03029db4c2799ae16b5428b06bd'
}

jeera
Updated on July 19, 2020Comments
-
jeera almost 3 years
I have a form-data as well as file to be sent in the same POST. For ex, {duration: 2000, file: test.wav}. I saw the many threads here on multipart/form-data posting using python requests. They were useful, especially this one.
My sample request is as below:
files = {'file': ('wavfile', open(filename, 'rb'))} data = {'duration': duration} headers = {'content-type': 'multipart/form-data'} r = self.session.post(url, files=files, data=data, headers=headers)
But when I execute the above code, I get this error:
5:59:55.338 Dbg 09900 [DEBUG] Resolving exception from handler [null]: org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found.
So my questions are: 1) How can I see the content of the request being sent? Couldn't use wireshark, its not across the network. 2) why is the boundary missing in the encoded data? Did I miss anything, please point out.
-
Dan almost 5 yearsHi, when I use postman to generate code it explicitly add that header but if I try if without the header it doesn't work. Perhaps you can spot my error / misunderstanding? stackoverflow.com/q/51141881/1011724
-
jmcgrath207 over 3 yearsto added to that I've had a requests session that was appending these headers. Once I removed it worked
api_session.headers.update({"Content-Type": "application/json"})
-
RCross over 2 yearsSorry, but that's not my experience. If I use curl, and don't set the Content-Type, it generates a Content-Type of Content-Type: multipart/form-data. When I try the same with Python Requests, it generates Content-Type: application/x-www-form-urlencoded which is wrong (and is rejected by the server I'm running the request against.
-
roamer over 2 yearssame solution to
missing initial multi part boundary
error -
Loi Nguyen Huynh about 2 yearsI encounter this error on occasion
{"detail":"Multipart form parse error - Invalid boundary in multipart: None"}
, Do you guys have ever encountered the same thing? It frustrates me really much. -
pdoherty926 over 1 yearWho is "we" in this answer?
-
semore_1267 over 1 yearThis is actually the correct answer for some reason. Using requests==2.26.0 and django-rest-framework==0.1.0. 8 years later. Imagine that
-
Ian Stapleton Cordasco over 1 yearWe referring to the maintainers of the requests library. The way it's authored, it needs to control this header. Specifying it yourself is documented as behaviour that should be avoided
-
FrostyOnion over 1 yearThis saved my life today. Thank you!
-
JEEVA RAJA PAUL over 1 yearThis saved tons of time. Many thanks.