Reputation: 15
I'm trying to upload a file to remote web site, using the CURL script it's working good
curl -X POST -H "Content-Type: multipart/form-data" -H "Authorization: Basic cWERF0ZWNoQXBpOmYxZDhmNzJkNDAwNGRjNzZlMTU0NjU4MTQwGRNzc4NTjM0" -F "[email protected]" http://url/rest/files/upload
but when I'm trying to use the Python requests lib i got this error:
600
org.jvnet.mimepull.MIMEParsingException: Missing start boundary
My python code:
import requests
headers = {
'Content-Type': 'multipart/form-data',
'Authorization': 'Basic cWERF0ZWNoQXBpOmYxZDhmNzJkNDAwNGRjNzZlMTU0NjU4MTQwGRNzc4NTjM0',}
files = {'file.dat':open('file.dat','rb')}
response = requests.post('http://url/rest/files/upload',headers=headers,files=files)
print response.text
Upvotes: 0
Views: 1161
Reputation: 797
You must avoid the 'Content-Type'
in your headers
dictionary. This way:
import requests
headers = {'Authorization': 'Basic cWERF0ZWNoQXBpOmYxZDhmNzJkNDAwNGRjNzZlMTU0NjU4MTQwGRNzc4NTjM0',}
files = {'file.dat':open('file.dat','rb')}
response = requests.post('http://url/rest/files/upload',headers=headers,files=files)
In this particular case (if the argument files
is specified) the library requests
adds the Content-Type: multipart/form-data
by itself, but with the specification of the boundary at the end. The header then looks for example like this:
Content-Type: multipart/form-data; boundary=e8c4b746efb54e2f9ae9412a4402714b
By adding 'Content-Type': 'multipart/form-data'
to the headers argument, the boundary is removed.
Upvotes: 1