Search code examples
pythonibm-cloudpycurl

Uploading a file via pyCurl


I am trying to convert the following curl code into pycurl. I do not want to use requests. I need to use pycurl because requests is not fully working in my old python version.

curl
-X POST
-H "Accept-Language: en"
-F "[email protected]"
-F "[email protected]"
"https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key={api-key}&version=2016-05-20"

Can someone please show me how to write it out in PyCurl?


Solution

  • import pycurl
    c = pycurl.Curl()
    c.setopt(c.URL, 'https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key={api-key}&version=2016-05-20')
    c.setopt(c.POST, 1)
    c.setopt(c.HTTPPOST, [("images_file", (c.FORM_FILE, "fruitbowl.jpg"))])
    c.setopt(c.HTTPPOST, [("parameters", (c.FORM_FILE, "myparams.json"))])
    c.setopt(pycurl.HTTPHEADER, ['Accept-Language: en'])
    c.perform()
    c.close()