47

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.

3 Answers 3

85

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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
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"})
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.
Who is "we" in this answer?
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
|
17

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

1 Comment

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
0

To specifically add boundary add following in header :

headers = {
    'content-type': 'multipart/form-data; boundary=ebf9f03029db4c2799ae16b5428b06bd'
}

2 Comments

See Ian Stapletons' answer above - you should never do this manually.
removing "content-type" did-not worked for me, adding boundary did.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.