2

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 2

10

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.

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

Comments

0

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

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?
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.

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.