使用python请求的多部分POST [英] Multipart POST using python requests

查看:74
本文介绍了使用python请求的多部分POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用python软件包请求进行了多部分POST. 我使用xlrd更改Excel文件中的某些值,然后将其保存,然后通过多部分POST发送. 当我在Mac上本地运行它,但将代码放在远程计算机上并发出相同的请求时,此内容工作正常,主体内容类型为空白,而在本地,主体内容类型为application/vnd.ms-excel.所以我的问题是,有没有一种方法可以使用python请求强制执行内容类型,以便在这种情况下主体内容类型为application/vnd.ms-excel. 对不起无法发布任何代码,因为我在这台机器上没有该代码.

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.

推荐答案

files参数接受元组键的字典,格式如下:

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', {})}

应该可以.

这篇关于使用python请求的多部分POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆