繁体   English   中英

卷曲可以工作,但是python请求无法上传图像

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM