Last active
November 29, 2016 21:24
-
-
Save AgustinPelaez/8541387 to your computer and use it in GitHub Desktop.
HTTP GET and POST example requests, compatible with Python 2.4. The GET request lists the user's data sources in Ubidots.
The POST request sends the value "33" to the variable "51eded72f91b285266ba30e9" in Ubidots.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib2 | |
response = urllib2.urlopen('http://things.ubidots.com/api/v1.6/datasources?token=9xtbvHL8hFKxfkmlXUW3qNoUpOEZAtp0qkkwPqffbn6DiR3ElRJ4B5m1$ | |
data = response.read() | |
print data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib2 | |
url = 'http://things.ubidots.com/api/v1.6/variables/51eded72f91b285266ba30e9/values?token=9xtbvHL8hFKxfkmlXUW3qNoUpOEZAtp0qkkwPqffbn6DiR3$ | |
data = '{"value":33}' | |
class BetterHTTPErrorProcessor(urllib2.BaseHandler): | |
# a substitute/supplement to urllib2.HTTPErrorProcessor | |
# that doesn't raise exceptions on status codes 201,204,206 | |
def http_error_201(self, request, response, code, msg, hdrs): | |
return response | |
def http_error_204(self, request, response, code, msg, hdrs): | |
return response | |
def http_error_206(self, request, response, code, msg, hdrs): | |
return response | |
opener = urllib2.build_opener(BetterHTTPErrorProcessor) | |
urllib2.install_opener(opener) | |
req = urllib2.Request(url, data, {'Content-Type': 'application/json'}) | |
response = urllib2.urlopen(req) | |
data = response.read() | |
print data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment