Skip to content

Instantly share code, notes, and snippets.

@AgustinPelaez
Last active November 29, 2016 21:24
Show Gist options
  • Save AgustinPelaez/8541387 to your computer and use it in GitHub Desktop.
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.
import urllib2
response = urllib2.urlopen('http://things.ubidots.com/api/v1.6/datasources?token=9xtbvHL8hFKxfkmlXUW3qNoUpOEZAtp0qkkwPqffbn6DiR3ElRJ4B5m1$
data = response.read()
print data
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