开发者

need help with moving from curl to pycurl

开发者 https://www.devze.com 2023-02-02 04:54 出处:网络
I\'开发者_StackOverflowm trying to figure out how to send a pycurl \"POST\" to a REST interface (in this case, Neo4j).

I'开发者_StackOverflowm 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号