Im making a multipart POST using the python package requests. Im using xlrd to change some values in an Excel file save it then send that up in a multipart POST. This working fine when I run it locally on my mac but when I put the code on a remote machine and make the same request the body content type is blank where as locally the body content type is application/vnd.ms-excel. So my question is, is there a way enforce the content type using python requests so that in this case the body content type is application/vnd.ms-excel. Sorry cant post up any code as I don't have it on this machine.
2 Answers
The files parameter accepts a dictionary of keys to tuples, with the following form:
files = {'name': (<filename>, <file object>, <content type>, <per-part headers>)}
In your specific case, you could write this:
files = {'file': ('filename.xls', open('filename.xls'), 'application/vnd.ms-excel', {})}
That should work fine.
Comments
I believe you can use the headers parameter, e.g
requests.post(url, data=my_data, headers={"Content-type": "application/vnd.ms-excel"})
2 Comments
user1946337
Will that not just add it as a header in http request but not in the body where that content-type is set based on the file type you send up?
Ian Stapleton Cordasco
That is not what he's asking for. Each part in a multipart request can have a content-type header. He's not looking to set the content type of the request as whole.