简体   繁体   中英

Multipart POST using python requests

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.

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.

我相信你可以使用headers参数,例如

requests.post(url, data=my_data, headers={"Content-type": "application/vnd.ms-excel"})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM