Testing APIs with Postman

An important part of API development is to test the APIs thoroughly before we expose them to the world, so we will use postman and test them one by one.

In this post we will be looking into testing APIs with Postman software. For details on how to create APIs in python with Django, check out my blog post here: https://sammyjoshua819336482.wordpress.com/2021/01/22/crud-apis-with-django/

We will be testing the APIs created in the above post with the Http methods, GET, POST PUT AND DELETE

We had created APIs to:

  1. Get All Employees
  2. Get Employee by ID
  3. Create a new employee
  4. Update an existing Employee
  5. Delete an existing employee

All these APIs will access the db and perform the respective operations.

Let’s get started.

  1. Get All Employees

Open Postman in your system and in the url field of postman type 127.0.0.1:8000/api/employees like below.

Creating requests

and press Send button on the right you would see the details of the employee you just added in the below box, I have marked it with Response from API in the above screenshots.

This output or response from API is in JSON format short for JavaScript Object Notation, which is the format used by most of the apis in the world to return a response, this is more human readable and also helps anyone who accesses your API to understand and interpret the data.

The response contains the id (primary key given by Django), first name and last name.

2. Get Employee by ID

From the previous api we can see that there is an id, first_name, last_name in the response, so to get the details of a particular employee, we must pass their id in the url, like this

127.0.0.1:8000/api/employees/1/ Now we will get only the details of the employee with the id(primary key) as 1, so the output will be:

Emplpyee 1 details

3. Create a new employee

To create a new employee, we must use the POST request, so in postman, change the request type to POST and put the url 127.0.0.1:8000/api/employees/,  and also pass the details in JSON format(like a python dictionary) in the body tab, and select raw radiobutton there and also change the drop-down to JSON, below this, The new user details to pass is:

{
"first_name":"Test",
"last_name":"User"
}

Take a look at the image shown below and see how it can be done.

Sending POST request

Now click on Send button, the response you would get back is the employee whose detail you have just added along with an id field added by django. Take a look at this response:

Response after adding Employee

Now you can check if this data is added to your table using a GET request like we did in the first point in this page on the 127.0.0.1:8000/api/employees/ .

4. Update an existing Employee

To update an existing employee info, you must send a PUT request along with the employee ID you want to update in url, you must also pass the updated employee details in the body.

Now, as we can see the id of the employee we just created is 3, so let us try updating their first name from ‘Test’ to ‘John’, to do this, change the request to PUT in postman first and give the url as  127.0.0.1:8000/api/employees/3/. You can have a look at the screenshot below:

Looking at the response, we can see that the employee record with the id of 3 is updated:

5. Delete an existing employee

To delete an employee, you must do the same steps as above except that you would not need to pass in any body in request, just the employee id you want to delete in the URL. So let us try to delete the employee with the id 3 that we just updated. To do this, change the request type to DELETE and the URL would be 127.0.0.1:8000/api/employees/3/ . But unlike the previous request this one does not return a response since the record is deleted. Have a look at this screenshot.

DELETE Employee

Now you can have a look at the admin page or go to the 127.0.0.1:8000/api/employees/ in postman and do a GET request to see the records to verify whether the record is deleted.

Thank you for spending some time to read my blog, please do leave a comment about how you feel about this blog and any improvements that should be done, also do share with your friends. Thanks again. 🙂

One thought on “Testing APIs with Postman

Leave a comment