Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python and urllib2: how to make a GET request with parameters

Tags:

python

urllib2

I'm building an "API API", it's basically a wrapper for a in house REST web service that the web app will be making a lot of requests to. Some of the web service calls need to be GET rather than post, but passing parameters.

Is there a "best practice" way to encode a dictionary into a query string? e.g.: ?foo=bar&bla=blah

I'm looking at the urllib2 docs, and it looks like it decides by itself wether to use POST or GET based on if you pass params or not, but maybe someone knows how to make it transform the params dictionary into a GET request.

Maybe there's a package for something like this out there? It would be great if it supported keep-alive, as the web server will be constantly requesting things from the REST service.

Ideally something that would also transform the XML into some kind of traversable python object.

Thanks!

like image 312
adamJLev Avatar asked May 11 '10 22:05

adamJLev


People also ask

How do you pass a query parameter in GET request Python?

To send parameters in URL, write all parameter key:value pairs to a dictionary and send them as params argument to any of the GET, POST, PUT, HEAD, DELETE or OPTIONS request. then https://somewebsite.com/?param1=value1&param2=value2 would be our final url.

Can a get request have query parameters?

You may use the queryParam() method not just once, but as many times as the number of query parameters in your GET request.

How do I use urllib2 in Python?

Simple urllib2 scripturlopen('http://python.org/') print "Response:", response # Get the URL. This gets the real URL. print "The URL is: ", response. geturl() # Getting the code print "This gets the code: ", response.


1 Answers

Is urllib.urlencode() not enough?

>>> import urllib >>> urllib.urlencode({'foo': 'bar', 'bla': 'blah'}) foo=bar&bla=blah 

EDIT:

You can also update the existing url:

  >>> import urlparse, urlencode   >>> url_dict = urlparse.parse_qs('a=b&c=d')   >>> url_dict   {'a': ['b'], 'c': ['d']}   >>> url_dict['a'].append('x')   >>> url_dict   {'a': ['b', 'x'], 'c': ['d']}   >>> urllib.urlencode(url_dict, True)   'a=b&a=x&c=d' 

Note that parse_qs function was in cgi package before Python 2.6

EDIT 23/04/2012:

You can also take a look at python-requests - it should kill urllibs eventually :)

like image 79
Tomasz Zieliński Avatar answered Sep 19 '22 01:09

Tomasz Zieliński