[英]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.