简体   繁体   中英

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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