Reputation: 3895
By default, it seems (for me) that every urlopen()
with parameters seems to send a POST request. How can I set the call to send a GET instead?
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
Upvotes: 10
Views: 18226
Reputation: 3108
The HTTP request will be a POST instead of a GET when the data parameter is provided.
Try urllib2.urlopen('http://httpbin.org/get?hello=there')
instead.
Upvotes: 4
Reputation: 13549
you could use, much the same way that post 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 supplied when making POST requests, such as when sending a application/x-www-form-urlencoded
content type, for example.
Upvotes: 12
Reputation: 6296
If you are making a GET request then you want to pass query string. You do that by placing a question-mark '?' at the end of your url before the params.
import urllib
import urllib2
params = urllib.urlencode(dict({'hello': 'there'}))
req = urllib2.urlopen('http://httpbin.org/get/?' + params)
req.read()
Upvotes: 2