Prerequisites

This is a follow-up series on how to use Python's Behave library to create automated Behavior Driven Tests. It assumes that you already have prior programming experience in Python and knows the basics of the HTTP Protocol.

Installation

We need to install the requests library and we need to set up the project workspace first. And that means we need to activate the python virtual environment and install requests using pip. If you don't have any idea on how to do it, I suggest you read my initial blog posts which covers how to setup Behave and install python virtual environments.

Once you've activated your virtual environment all you need to do is to run the below command and it will install the requests library.

pip3 install requests

How to make REST API calls

We will be using this fake api service that covers the mostly used http methods so to do that create a .py file and type the following code snippet and save it on your desired directory.

Get Request

import requests

response = requests.get('http://dummy.restapiexample.com/api/v1/employees')

print(response.status_code)
print(response.json())

Running the script will output two things. First is the status code and then the response body. We need to take note that response.status_code will return an int type of the HTTP status code and response.json() will convert the response body to a format that is known to python, and on this case a list.

Post Request

import requests

req_body = {'name': 'kyle pogi',
                'salary': '3232323',
                'age': '34'}

response = requests.post('http://dummy.restapiexample.com/api/v1/create', json=req_body)

print(response.status_code)
print(response.json())

Sending a post request is fairly similar to using a get request and the only difference is that you've passed a request_body to the post method. On this example, the endpoint http://dummy.restapiexample.com/v1/create needs to have a request body which is why we passed the request_body dictionary to the json keyword parameter. The contents of req_body is then saved to the database and can be retrieved via get request. If I run the script above it displays on what you see below.

200
{'name': 'kyle pogi', 'age': '34', 'salary': '3232323', 'id': '2407'}

Again it's very similar to the previous get request example which returns the response code and response body. One detail that you might have noticed is that it returned  'id':'2407'. This is most likely auto generated by the REST api.

Put Request

import requests

req_body = {'name': 'kyle pogi',
                'salary': '3232323',
                'age': '29'}

response = requests.put('http://dummy.restapiexample.com/api/v1/update/2407', json=req_body)

print(response.status_code)
print(response.json())

One key difference between the put and post is that the URL path has changed. We used the 'id':'2407' returned by the post request to specify which record we want to update. If you look closely at the req_body is that I've changed the age to 29 and we should expect it to be different if we run the snippet.

Delete Request

import requests

response = requests.delete('http://dummy.restapiexample.com/api/v1/delete/300')

print(response.status_code)
print(response.json())

To delete a record you just need the id that was returned when you triggered the post request and add it to the url path. Delete requests will most likely not return any value in response.json()

The snippets that I've written follows the api contracts which is listed on the frontpage of the site. If you don't implement it the way it was designed then you won't be getting your desired outcome.

There's still a lot of things that I didn't cover here but these snippets will give you a head start where you can start exploring. One cool tip that I can share with you is to get back and understand how HTTP works and then explore the rest of the requests module. The package documentation is really superb and I suggest that you try things out by experimenting with different API calls with different headers or query parameters. The json keyword parameter is just one of the few things that will help you out in passing requests body in JSON format but it's just the tip of the iceberg.

Next Steps

Now that we've set up the foundation, we can now start writing feature files and code the iplementation in python.