user1767754
user1767754

Reputation: 25094

set filename on Post with python requests

I want to set the name of a file I'm uploading via requests module.

files = {'filename': open('myfileXXAAAZZZD','rb')}
r = requests.post("http://127.0.0.1:5000/", files=files)

It is possible with curl but don't know if requests support's this:

Upvotes: 10

Views: 11016

Answers (1)

user2390182
user2390182

Reputation: 73470

As stated in the docs, the values of the files dict can be tuples with filename and content as the first of possibly more elements. So something like

files = {'fieldname': ('filename.any', open('myfileXXAAAZZZD','rb').read())}

should do the trick.

Upvotes: 22

Related Questions