Python – Sending HTTP GET request using urllib

httprequestpythonurlliburllib2

I am trying to send HTTP GET request using urllib/urllib2 with some data.

If we set some value of data param in urllib2.urlopen(url, data) the request object is changed to send POST request instead of GET.

Is there any way to achieve this? Standard or Hack?

Code snippet,

import requests
import urllib
query = urllib.urlencode({'query':'["=", ["fact", "role"], "storage"]'})
# using request object
print 'Output 1.'
response = requests.get("http://localhost:8082/v3/nodes", data=query)
print response.json()
print
# using urllib object
print 'Output 2.'
resp =  urllib.urlopen('http://localhost:8082/v3/nodes', data=query)
print resp.read()

Output:

Output 1. 
[{u'deactivated': None, u'facts_timestamp': u'2016-02-04T14:06:07.269Z', u'name': u'node_xx_11', u'report_timestamp': None, u'catalog_timestamp': u'2016-02-04T14:06:16.958Z'}, {u'deactivated': None, u'facts_timestamp': u'2016-02-04T14:06:05.865Z', u'name': u'node_xx_12', u'report_timestamp': None, u'catalog_timestamp': u'2016-02-04T14:06:13.614Z'}]

Output 2.
The POST method is not allowed for /v3/nodes

For the
References I have gone through,

https://docs.python.org/2/library/urllib2.html#urllib2.urlopen

https://docs.python.org/2/library/urllib2.html#urllib2.Request.add_data

This is not the road block for me, as I am able to use requests module for sending the data with GET request type. Curiosity is the reason of this post.

Best Answer

The data parameter of urlopen is used to set the body of the request. GET requests cannot contain a body, as they should only be used to return a resource, which should only be defined by it's URL.

If you need to pass parameters, you can append them to the url, in your case :

urllib.urlopen('http://localhost:8082/v3/nodes?{}'.format(query))