0

I have this curl command to execute by means of pycurl library:

curl https://lipn.univ-paris13.fr/framester/en/wfd_json/sentence -d "data=Remember the milk:t" -X PUT

and this is my pycurl conversion:

url = "https://lipn.univ-paris13.fr/framester/en/wfd_json/sentence"
buffer = StringIO()
curl = pycurl.Curl()
curl.setopt(pycurl.CAINFO, certifi.where())
curl.setopt(curl.URL, url)
pycurl.HTTPHEADER, ['Content-Type: application/json' ,'Accept: application/json']
curl.setopt(pycurl.CUSTOMREQUEST, "PUT")
curl.setopt(pycurl.POST, 1)
curl.setopt(pycurl.VERBOSE, 1)
data = urllib.urlencode({"data":"Remember the milk:t"})
curl.setopt(pycurl.POSTFIELDS, data)
curl.setopt(curl.WRITEFUNCTION, buffer.write)
curl.perform()
curl.close()
body = buffer.getvalue()
print(buffer.getvalue())

The problem is that the curl command returns a json, while when I execute through pycurl the elaboration doesn't stop. Can someone help me to correct my code? Why the python code doesn't stop? Thank you

1 Answers1

0

In your code snippet, HTTPHEADER option was not set correctly, Below version of the script executes fine.

import pycurl
import certifi
import json
import StringIO

TARGET_URL = "https://lipn.univ-paris13.fr/framester/en/wfd_json/sentence"
data = json.dumps({"data": "Remember the milk:t"})

c = pycurl.Curl()
c.setopt(c.URL, TARGET_URL)
c.setopt(pycurl.CAINFO, certifi.where())
c.setopt(pycurl.CUSTOMREQUEST, "PUT")
c.setopt(pycurl.POST, True)
c.setopt(pycurl.POSTFIELDS, data)
c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json'])
try:
    # c.setopt(pycurl.VERBOSE, 1)
    buf = StringIO.StringIO()
    c.setopt(c.WRITEFUNCTION, buf.write)
    print("(info) Invoking PUT on configured target-url...")
    c.perform()
    body = buf.getvalue().decode(encoding="utf-8")
    print("(info) Response => \"{}\"".format(body))
    c.close()
except pycurl.error, error:
    errno, errstr = error
    print('An error occurred:', errstr)

Above script execution prints data show below:

(info) Invoking PUT on configured target-url...
(info) Response => "There are no similar frames."

Process finished with exit code 0
Saumil
  • 156
  • 2