42

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.

Community
  • 1
  • 1
jeera
  • 591
  • 1
  • 7
  • 15

3 Answers3

79

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.

Ian Stapleton Cordasco
  • 26,944
  • 4
  • 67
  • 72
  • 2
    Hi, 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? https://stackoverflow.com/q/51141881/1011724 – Dan Jul 03 '18 at 12:00
  • to 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"})` – jmcgrath207 Oct 16 '19 at 22:15
  • 3
    Sorry, 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. – RCross Sep 08 '20 at 17:08
  • same solution to `missing initial multi part boundary` error – roamer Jan 05 '21 at 07:17
  • I 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. – Loi Nguyen Huynh Apr 05 '21 at 16:15
  • 2
    Who is "we" in this answer? – pdoherty926 Oct 29 '21 at 17:20
  • 1
    We 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 – Ian Stapleton Cordasco Oct 31 '21 at 00:33
12

Taking out the Content-Type header with explicit "multipart/form-data" worked!

Anirban Kundu
  • 131
  • 1
  • 6
  • This 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 – semore_1267 Oct 29 '21 at 23:40
0

To specifically add boundary add following in header :

headers = {
    'content-type': 'multipart/form-data; boundary=ebf9f03029db4c2799ae16b5428b06bd'
}
jeet.chanchawat
  • 5,842
  • 5
  • 38
  • 59