How to set GET parameters with PyCurl?

Andrew picture Andrew · Mar 17, 2011 · Viewed 12.3k times · Source

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 get 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.

Answer

Mahmoud Abdelkader picture Mahmoud Abdelkader · Mar 17, 2011

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 ...