![](/img/trans.png)
[英]GET request works with postman but not with python requests and curl
[英]file upload works with curl and postman but not python requests
我正在尝试使用python请求库将CSV文件上传到API(该错误信息很少)。
(我正在运行Python 3.5,并在OS X 10.11.6上使用请求版本2.18.4)
终端中的curl命令可以正常工作: curl -F 'file=@/path/to/file.csv' myurl.com/upload -H "Authorization: TOKEN sometoken"
来自Postman的多部分/表单数据POST请求也可以使用,但是我似乎无法使其与python请求库一起使用。
我已经尝试了此请求的许多变体:
import requests
headers = {'Authorization': 'TOKEN sometoken', 'Content-Type': 'multipart/form-data'}
with open(file_path, 'rb') as f:
r = requests.post(myurl, headers=headers, data=f)
## I've also tried data={"file": f}
我得到的状态码为200,但响应为{"success": "false"}
(非常无助)。
与curl请求相比,我在python请求中缺少什么?
编辑: curl
命令的-F
标志似乎模拟了已提交的HTML表单...是否可以通过请求执行此操作?
在您的curl
代码中,您使用-F
参数,该参数将数据作为多部分消息提交-换句话说,您正在上传文件。
对于requests
您可以使用files
参数发布文件。 一个例子:
import requests
headers = {'Authorization': 'TOKEN sometoken'}
data = {'file': open(file_path, 'rb')}
r = requests.post(myurl, headers=headers, files=data)
请注意, requests
将根据提交的数据自动创建Content-Type标头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.