How to create a GET request with parameters?

By default it seems (to me) that every urlopen() with parameters seems to send a POST request. How can I set up a call to send a GET?

 import urllib import urllib2 params = urllib.urlencode(dict({'hello': 'there'})) urllib2.urlopen('http://httpbin.org/get', params) 

urllib2.HTTPError: HTTP Error 405: METHOD NOT ALLOWED

+7
source share
3 answers

you can use the same as after the request:

 import urllib import urllib2 params = urllib.urlencode({'hello':'there', 'foo': 'bar'}) urllib2.urlopen('http://somesite.com/get?' + params) 

The second argument should only be provided when making POST requests, for example, when sending the content type application/x-www-form-urlencoded .

+9
source

The HTTP request will be POST instead of GET when a data parameter is provided. Try urllib2.urlopen('http://httpbin.org/get?hello=there')

+3
source

If you are making a GET request, you want to pass the request string. You do this by placing the question mark '?' at the end of your URL in front of the parameters.

 import urllib import urllib2 params = urllib.urlencode(dict({'hello': 'there'})) req = urllib2.urlopen('http://httpbin.org/get/?' + params) req.read() 
+1
source

All Articles