Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 819
Andrew Avatar asked Mar 17 '11 21:03

Andrew


2 Answers

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 ...
like image 67
Mahmoud Abdelkader Avatar answered Nov 11 '22 00:11

Mahmoud Abdelkader


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&param2=value2

where you should replace param1, param2, value1 and value2 with whichever values you have stored in post_data.

like image 44
Jong Bor Lee Avatar answered Nov 11 '22 01:11

Jong Bor Lee