multipart data POST using python requests: no multipart boundary was found

37,662

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'
}
Share:
37,662
jeera
Author by

jeera

Updated on July 19, 2020

Comments

  • jeera
    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
    Dan almost 5 years
    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? stackoverflow.com/q/51141881/1011724
  • jmcgrath207
    jmcgrath207 over 3 years
    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"})
  • RCross
    RCross over 2 years
    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.
  • roamer
    roamer over 2 years
    same solution to missing initial multi part boundary error
  • Loi Nguyen Huynh
    Loi Nguyen Huynh about 2 years
    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.
  • pdoherty926
    pdoherty926 over 1 year
    Who is "we" in this answer?
  • semore_1267
    semore_1267 over 1 year
    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
  • Ian Stapleton Cordasco
    Ian Stapleton Cordasco over 1 year
    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
  • FrostyOnion
    FrostyOnion over 1 year
    This saved my life today. Thank you!
  • JEEVA RAJA PAUL
    JEEVA RAJA PAUL over 1 year
    This saved tons of time. Many thanks.