Python请求,没有文件的多部分

标签 python python-requests

是否可以在不发送文件的情况下使用 python 请求发送 multipart/form-data?

我的请求头应该是这样的:

--3eeaadbfda0441b8be821bbed2962e4d
Content-Disposition: form-data; name="value";

content
--3eeaadbfda0441b8be821bbed2962e4d--

但实际上它看起来像这样:

--3eeaadbfda0441b8be821bbed2962e4d
Content-Disposition: form-data; name="file"; filename="file"
Content-Type: text/plain

content
--3eeaadbfda0441b8be821bbed2962e4d--

我正在使用这个函数:

response = requests.post('url', data, files=('file', 'file'))

最佳答案

是的,你可以,只需将带有元组的字典作为值传递给 post 方法。当您为此方法 requests 指定 files 参数时,添加 multipart/form-data header 。但是你可以自由地将带有元组的字典作为 files 传递:

url = 'http://httpbin.org/post' 
files = {'file': (None, 'some,data,to,send\nanother,row,to,send\n')}
r = requests.post(url, files=files) 

http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file

关于Python请求,没有文件的多部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34451685/

相关文章:

python - 如何拆分字符串并转换为整数

python - 查找两个日期之间月份的最​​佳方法

python - 不太明白如何使用 python 请求执行谷歌服务器的请求

python - 直接从 Python 3 模块导入函数

python - 如何将Python对象类型(应该是Python日期类型)列转换为Python日期列?

python - 如何在Python中使用登录 session 进行另一个http请求?

python-2.7 - 用于 Python 的 Bittrex REST API,我想使用 API v3 https ://api. bittrex.com/v3/orders 创建订单

python - url :/Caused by ProxyError 超出了最大重试次数

python - 请求在表单上发布不返回生成的页面

python - 如何轻松分发具有 Python 模块依赖项的 Python 软件?在 Unix 上安装 Python 包的挫折