简体   繁体   中英

need help with moving from curl to pycurl

I'm trying to figure out how to send a pycurl "POST" to a REST interface (in this case, Neo4j).

I can do it via CURL with:

curl -HContent-Type:application/json -X POST -d '"http://localhost:7474/db/data/node/123"' http://localhost:7474/db/data/index/node/persons/name/peter

... but I cannot figure out how to translate that with CURL options. This is the closest that I've gotten but it fails

data = "http://localhost:7474/db/data/node/123"
headers = {"Content-Type": "application/json"}
url_dir = "http://localhost:7474/db/data/index/node/persons/name/peter"

conn = pycurl.Curl()
c.setopt(pycurl.URL, url_dir)
c.setopt(pycurl.HTTPHEADER, headers)

what do I do with the data to transmit?

edit: by data I mean the variable entitled data. How do I send the data (what's included in the -d option) via pycurl. I've tried urlencode-ing it but get an error (obviously, because of its type). I've tried urllib.quote_plus() but that's caused a 400 error on the REST service. I suspect that this is the issue since my GETs are working just fine.

Appending these statements may help:

c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, data)

Check out the "HTTP POSTing" section of the libcurl tutorial for more info.

Note 1: If the data string must be enclosed by double quotes, then you should do: c.setopt(pycurl.POSTFIELDS, '"%s"' % data) .

Note 2: I think you made a typo: the conn variable must be called c .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM