0
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?

deppfx
  • 701
  • 1
  • 10
  • 24

1 Answers1

1

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.

Selcuk
  • 57,004
  • 12
  • 102
  • 110
  • Still a 404. Is there a way I can get more information? Turn on debugging or something? – deppfx Feb 04 '19 at 06:29
  • Weird, is this a documented API? You can enable logging in `requests`, see [this answer](https://stackoverflow.com/a/16630836/2011147). – Selcuk Feb 04 '19 at 06:30
  • It's undocumented, but I'm guesing I'm doing something wrong on the way I pass the Authorization Bearer token. – deppfx Feb 04 '19 at 07:45
  • It is strange that it works with `curl` though. Make sure that you are passing the exact same arguments. Also try faking the user agent by adding the `User-Agent` header. – Selcuk Feb 04 '19 at 12:31