How to set GET parameters with PyCurl?
I'm trying to make a GET request for a REST api using PycURL. I am able to successful make a request if I do not pass any parameters. I am also able to make a POST request by doing the following:
curl.setopt(pycurl.POSTFIELDS, post_data)
I want to make a 开发者_StackOverflowget request that includes login parameters. If I try to use the line above to pass the parameters, it tries to make a POST instead. I can't figure out how to set the login parameters and make a GET request.
Any help would be appreciated.
Just like a normal URL on the browser, GET parameters are encoded and appended after a ?
to the URL. Using python's urllib.urlencode, you can do:
import urllib
import pycurl
url = 'http://www.google.com/search'
params = {'q': 'stackoverflow answers'}
c = pycurl.Curl()
c.setopt(c.URL, url + '?' + urllib.urlencode(params))
... # and so on and so forth ...
There seems to be no pycurl.GETFIELDS option, so you'll have to pass these parameters in the url, like this:
http://your_url/some_path?param1=value1¶m2=value2
where you should replace param1, param2, value1 and value2 with whichever values you have stored in post_data
.
精彩评论