1

I am developing with apis however I faced with a problem that I have never met.

curl -F "media=@IMAGE_NAME" 'xxxx url'

How do I convert it into python code with requests?

Michael Szczesny
  • 4,911
  • 5
  • 15
  • 32
Octavian
  • 124
  • 2
  • 9

3 Answers3

2

There's a great example of a POST request in the manual. I think yours specifically would be:

r = requests.post("xxx url", data={"media": "@IMAGE_NAME"})
  • Yes, I've known this usage. Actually I am not understanding what @IMAGE_NAME means. In a web app, Does it mean the file itself or the file name I got from the form? – Octavian May 26 '15 at 06:19
  • Sorry, I have ready known that files = { 'media': open(image_name, 'rb') } – Octavian May 27 '15 at 02:53
2

I solved this problem.

files = { 'media': open(image_name, 'rb') }

Then

requests.post(url, files=files)

see http://www.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file

Octavian
  • 124
  • 2
  • 9
-1

As per your case :

def command_string_generator(method, token, port, account,container=None, obj=None, headers_in=None, curlhead=None):
    url = 'xxxx url'
    image = "media=@IMAGE_NAME"
    command_string = 'curl -F %s %s ' % (image ,url)
    print command_string
    return command_string

you can use a function like this as per your needs with -F and change accordingly. Hope this might be helpful.Additional parameters are just for convienience.