我可以在Python中使用form/multipart发布一个文件数组吗?

2024-06-28 11:53:18 发布

您现在位置:Python中文网/ 问答频道 /正文

例如:我需要以这样的格式发布数据:{imgFiles:[(filename,file),(filename,file),(flename file)]

我试过这样做:

pic_array = [
    ('file1', open("somefile.xml", "r")),
    ('file2', open("somefile2.xml", "r"))
]
files_pics = [('imgFiles', pic_array)]

r = requests.post(
    'https://some.site/path/to/api/point',
    data=data_details,
    headers=headers_1,
    files=files_pics
)
print(r.status_code, r.reason, r.json())

然后得到一个

^{pr2}$

有什么方法可以完全按照数组来发布文件吗?在


Tags: 数据data格式filesxmlopenfilenamearray
1条回答
网友
1楼 · 发布于 2024-06-28 11:53:18

您错误地发送了多个文件请参阅以下工作示例。如果我拿我的例子,把它改成像你这样的列表,我会得到同样的错误。在

import requests

url = 'http://localhost:8080/'
files = {'file':  open('sql.py', 'rb'),
         'file2': open('lst.py', 'rb')}

r = requests.post(url, files=files)
print(r.text)

相关问题 更多 >