[英]Upload a file using python requests
我在尝试将文件上传到 API 时遇到问题。 在 swagger UI 中,我手动上传 excel 文件没有问题。 当我尝试使用请求上传时,我收到 415 错误(我的文件格式无效)。 这是该发布请求的简单代码:
headers = {
'Authorization':"bearer "+ CLIENT.token,
'Content-Type': 'form-data'
}
files = [('file', open(path_to_file, 'rb'))]
response = requests.post(api_url,
files=files,
headers=headers)
我的回复有状态代码 415,我不知道发生了什么。 当我使用swagger时,我检查了newtwork浏览器,在请求中看到了这个header
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarywkrdGd3ROh0GkfsW
但是,我不知道“边界”一词是什么,如果我手动将此标头传递到请求中,API 会抛出 500。
服务器说您的Content-Type
是错误的。 如果您要上传.xls
文件,请使用:
'Content-Type': 'application/vnd.ms-excel'
如果您要上传.xlsx
文件,请使用:
'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.