Ars*_*nko 3 python multipartform-data http-post python-3.x python-requests
随着requests使用POST简单的数据时,我可以为多个值使用相同的名称.CURL命令:
curl --data "source=contents1&source=contents2" example.com
Run Code Online (Sandbox Code Playgroud)
可以翻译成:
data = {'source': ['contents1', 'contents2']}
requests.post('example.com', data)
Run Code Online (Sandbox Code Playgroud)
这同样适用于文件.如果我翻译工作CURL命令:
curl --form "source=@./file1.txt" --form "source=@./file2.txt" example.com
Run Code Online (Sandbox Code Playgroud)
至:
with open('file1.txt') as f1, open('file2.txt') as f2:
files = {'source': [f1, f2]}
requests.post('example.com', files=files)
Run Code Online (Sandbox Code Playgroud)
只收到最后一个文件.
MultiDict来自werkzeug.datastructures也无济于事.
如何提交具有相同POST名称的多个文件?
不要使用字典,使用元组列表; 每个元组一(name, file)对:
files = [('source', f1), ('source', f2)]
Run Code Online (Sandbox Code Playgroud)
该file元素可以是另一个元组,其中包含有关该文件的更多详细信息; 要包含文件名和mimetype,您可以:
files = [
('source', ('f1.ext', f1, 'application/x-example-mimetype'),
('source', ('f2.ext', f2, 'application/x-example-mimetype'),
]
Run Code Online (Sandbox Code Playgroud)
这在文档的Advanced Usage一章的POST Multiple Multipart-Encoded Files部分中有记录.
| 归档时间: |
|
| 查看次数: |
3295 次 |
| 最近记录: |