Skip to content

Instantly share code, notes, and snippets.

@sentenza
Last active December 18, 2015 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sentenza/5719857 to your computer and use it in GitHub Desktop.
Save sentenza/5719857 to your computer and use it in GitHub Desktop.
Simple Python REST client See also https://github.com/scastillo/siesta
import httplib2 as http
import json
try:
from urlparse import urlparse
except ImportError:
from urllib.parse import urlparse
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json; charset=UTF-8'
}
uri = 'http://yourservice.com'
path = '/path/to/resource/'
target = urlparse(uri+path)
method = 'GET'
body = ''
h = http.Http()
# If you need authentication some example:
if auth:
h.add_credentials(auth.user, auth.password)
response, content = h.request(
target.geturl(),
method,
body,
headers)
# assume that content is a json reply
# parse content with the json module
data = json.loads(content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment