![](/img/trans.png)
[英]file upload works with curl and postman but not python requests
[英]Curl works but python requests is failing to upload an image
curl 'https://example.com/v2/' -F 'master=@test.jpg;type=image/JPEG' -H 'X-Generate-Renditions: all' -H 'X-Create-Asset-Sync: 1' -H 'Authorization: Bearer xyz' -H 'X-Read-Meta: none'
可以顺利运行,但以下python请求代码返回的不是404。
import requests
headers = {
'X-Generate-Renditions': 'all',
'X-Create-Asset-Sync': '1',
'Authorization': 'Bearer xyz',
'X-Read-Meta': 'none'
}
with open('test.jpg', 'rb') as f:
response = requests.post('https://example.com/v2/', headers=headers, files={'test.jpg': f})
print(response.status_code)
返回404。
我究竟做错了什么?
您没有使用正确的字段名发送文件。 更改以下部分
files={'test.jpg': f}
至
files={'master': ('test.jpg', f, 'image/JPEG')}
有关正确用法,请参见http://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.