API Testing with Python (Requests Module).

What is API Testing?

API testing is a type of software testing which involves testing application programming interface or software resource as part of product integration testing to determine if they meet expectations or functionality, reliability, performance, and security.

What are types of API?

Simple Object Access Protocol (SOAP)

Remote Procedure Call (RPC)

Representational State Transfer (REST)

Out of above three, last one has become now industry standard API implementation with many feature ready standards and also it has support for many programming languages.

Python Request Module for testing API

Request is a Python Module for testing application programming interface with features of get , post, put and delete.

The Requests module is a an elegant and simple HTTP library for Python.

Simple Example:-

Make a Request

import requests

r = requests. get (‘https://api.github.com/events’)

r = requests.post (‘https: //httpbin.org/post’, data = {‘key’:’value’})

r = requests. put (‘https://httpbin.org/put’, data = {‘key’:’value’})

r = requests. delete (‘https://httpbin.org/delete’)

r = requests. head (‘https://httpbin.org/get’)

r = requests. options (‘https://httpbin.org/get’)

r = requests. options (‘https://httpbin.org/get’)

Passing Parameters in URL:

payload ={‘key1’:’value1’,’key2’:’value2’}

r = requests. get (‘https://api.github.com/events’, params=payload)

Response Content:

r = requests. get (‘https://api.github.com/events’)

r.text

[{u’repository’: {u’open_issues’: 0, u’url’: ‘https://github.com…..

r.encoding

utf-8

r.encoding = ‘ISO-8859-1’

JSON Response Content:

r = requests. get (‘https://api.github.com/events’)

r.json()

[{u’repository’: {u’open_issues’: 0, u’url’: ‘https://github.com…..

For Complete Requests Module  Reference, Check the below links.

http://docs.python-requests.org/en/master/

https://pypi.python.org/pypi/requests

REST vs SOAP Web Services

I am seeing a lot of new web services are implemented using a REST style architecture these days rather than a SOAP one. Lets step back a second and explain what REST is.

What is a REST Web Service

The acronym REST stands for Representational State Transfer, this basically means that each unique URL is a representation of some object. You can get the contents of that object using an HTTP GET, to delete it, you then might use a POST, PUT, or DELETE to modify the object (in practice most of the services use a POST for this).

Who’s using REST?

All of Yahoo’s web services use REST, including Flickr, del.icio.us API uses it, pubsub, bloglines, technorati, and both eBay, and Amazon have web services for both REST and SOAP.

Who’s using SOAP?

Google seams to be consistent in implementing their web services to use SOAP, with the exception of Blogger, which uses XML-RPC. You will find SOAP web services in lots of enterprise software as well.

REST vs SOAP

As you may have noticed the companies I mentioned that are using REST api’s haven’t been around for very long, and their apis came out this year mostly. So REST is definitely the trendy way to create a web service, if creating web services could ever be trendy (lets face it you use soap to wash, and you rest when your tired). The main advantages of REST web services are:

Lightweight – not a lot of extra xml markup Human Readable Results Easy to build – no toolkits required SOAP also has some advantages:

Easy to consume – sometimes Rigid – type checking, adheres to a contract Development tools For consuming web services, its sometimes a toss up between which is easier. For instance Google’s AdWords web service is really hard to consume (in CF anyways), it uses SOAP headers, and a number of other things that make it kind of difficult. On the converse, Amazon’s REST web service can sometimes be tricky to parse because it can be highly nested, and the result schema can vary quite a bit based on what you search for.

Which ever architecture you choose make sure its easy for developers to access it, and well documented.