1

I'm beginning to expand my python knowledge and am attempting to consume (e.g. update and receive data from a database with http POST requests with XML payloads) a REST API. I'm well aware of how to consume a REST API, but am a bit unsure what libraries to use with regards to Python specifically.

Is urllib the way to go? the requests module? django (of which I'm entirely naïve to)?

This is not intended to be a subjective answer filled with opinions, but a simple introduction and a point in the right direction on how to use urllib (or others) in conjunction with REST API.

How do you consume a REST Service with Python?

Brandon Deo
  • 4,195
  • 4
  • 25
  • 42
  • Does this http://stackoverflow.com/questions/4355997/is-there-a-generic-python-library-to-consume-rest-based-services help at all? – Paul Rooney Jan 28 '16 at 02:57
  • @PaulRooney not particularly because i'm still unsure how to consume it with the use of the library. – Brandon Deo Jan 28 '16 at 03:03
  • what do you mean "consume"? can you be more specific about you're trying to do? you should be able to use `requests.get`, `requests.post` etc. – maxymoo Jan 28 '16 at 03:45
  • What do you need? Making a http call and handle the response or something more? – alvinzoo Jan 28 '16 at 03:58

4 Answers4

3

There are many python libraries that You can use to make REST call, famous among them is Requests.

Sample GET call

import requests

def consumeGETRequestSync():
 params = {'test1':'param1','test2':'param2'}
 url = 'http://httpbin.org/get'
 headers = {"Accept": "application/json"}
 # call get service with headers and params
 response = requests.get(url, headers = headers,data = params)
 print "code:"+ str(response.status_code)
 print "******************"
 print "headers:"+ str(response.headers)
 print "******************"
 print "content:"+ str(response.text)

consumeGETRequestSync()

Sample POST call

import requests


def consumePOSTRequestSync():
 params = {'test1':'param1','test2':'param2'}
 url = 'http://httpbin.org/post'
 headers = {"Accept": "application/json"}
 # call post service with headers and params
 response = requests.post(url,headers= headers,data = params)
 print "code:"+ str(response.status_code)
 print "******************"
 print "headers:"+ str(response.headers)
 print "******************"
 print "content:"+ str(response.text)

# call 
consumePOSTRequestSync()

You can check out this link http://stackandqueue.com/?p=75 for more details

gvir
  • 256
  • 3
  • 4
1

Hi I am assuming that by stating REST Consumption you meant to use a REST api as a client ( doing GET requests or PUSH )

This might be my personal preference, but I always use the requests library to do my http calls

Once I get a respond, depending on the type of results, I will parse it with either the built-in json library or with beautifulsoup

Consuming a REST API that returns JSON results is great because a json can be decoded ( loaded ) into a python dictionary easily. ( a python dictionary has the same structure as json - sort of )

I am going to give you an example with a GET request with a JSON response because it is easier but you can find example of POST request get easily as well

import requests 
import json 

dest = 'https://github.com/timeline.json' 
## make the get request and store response
res = requests.get(dest)
## you might want to check if respond code is correct by accessing attribute status_code 


## Get the body of the respond as text 
body = res.text 
## load json string ( text ) into dictionary 
res_dict = json.loads(body) 

## json is now a dict
assert 'messages' in res_dict
assert 'documentation_url' in res_dict 
biobirdman
  • 4,060
  • 1
  • 17
  • 15
0

Both urllib2 and requests will do the work. If it's just a web service API, simply make a http call.

requests module is the better choice for JSON responses since urllib2 doesn't come with a native JSON serializer which requests has. For XML responses, you will have to use a external XML parser(such as minidom). Regarding making http calls, requests and urllib2 are really not that different. A comparison is here(What are the differences between the urllib, urllib2, and requests module?) but actually they are quite exchangable.

Django is a web service framework and is on an entirely different level. Django applications can be both service provider and client of REST APIs but Django doesn't come with it natively. You will still have to build the functionalities with other tools. You can use django-rest-framework to build your own REST APIs or call 3rd-party the same way with request.

Community
  • 1
  • 1
alvinzoo
  • 493
  • 2
  • 8
  • 17
0

For a REST Client I would also recommend requests, I would have just made this a comment on biobirdman's answer but don't have enough rep.

If you are working with json you don't need to import the json module, and you can send a straight dict object and parse the json response back into a dict like this: eg.

import requests

uri = 'https://myendpoint/get_details_from_id'
json_body={'id' : '123', 'attribute' : 'first_name'}

resp = requests.post(uri,
                     json=json_body)

response_as_dict=resp.json()

Hope that helps.