【问题标题】:python requests chunked encoding file and metadatapython请求分块编码文件和元数据
【发布时间】:2020-02-05 08:27:16
【问题描述】:
如何使用分块编码发送表单数据和文件?
我试过了:
def gen():
a = f.read(1024)
while a:
yield a
a = f.read(1024)
r = requests.post(url, data=gen())
它确实是使用分块编码发送文件。但是我不知道如何以 { "key" : "value" } 格式附加到这个生成器文件名和表单数据。
【问题讨论】:
标签:
python
python-requests
chunked-encoding
【解决方案1】:
看起来我使用 requests_toolbelt 解决了它:
from requests_toolbelt import MultipartEncoder
m = MultipartEncoder(
fields = {
"key": "value",
"file1": ("my_file.zip", open("my_file.zip", "rb")),
}
)
def gen():
a = m.read(1024)
while a:
yield a
a = m.read(1024)
r = requests.post(url, data=gen(), headers={'Content-Type': m.content_type})