首页 > 解决方案 > python通过请求上传文件

问题描述

当我使用 curl 将数据上传到服务器时,一切正常,但是当我在 python 中使用请求时,api 服务器不接受请求。

块引用

卷曲:

curl -X POST   https://tapi.bale.ai/botXXX/Senddocument \
-H 'content-type: multipart/form-data' \
-F chat_id=2040137555 \
-F document=@/var/tmp/2706021400.pdf \
-F caption=caption

要求:

import requests

headers = {
    'content-type': 'multipart/form-data',
}
files = {
    'chat_id': (None, '2040137555'),
    'document': ('/var/tmp/2706021400.pdf', open('/var/tmp/2706021400.pdf', 'rb')),
    'caption': (None, 'caption'),
}
response = requests.post('https://tapi.bale.ai/botXXX/Senddocument', headers=headers, files=files)

标签: pythoncurlpython-requests

解决方案


推荐阅读