[英]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'
works without a hitch, but not the below python requests code returns 404. 可以顺利运行,但以下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)
Returns a 404. 返回404。
What am I doing wrong? 我究竟做错了什么?
You are not sending the file with the correct field name. 您没有使用正确的字段名发送文件。 Change the following part 更改以下部分
files={'test.jpg': f}
to 至
files={'master': ('test.jpg', f, 'image/JPEG')}
See http://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file for the correct usage. 有关正确用法,请参见http://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.