API TESTING IN POSTMAN

This article is written to provide the basic knowledge of API testing using Postman . I will also touch a little on Swagger to ensure you have a comprehensive picture how a tester extracts url from Swagger and implements it in Postman. For a start, you can click on the icon below to download Postman for free.

Click to download
Postman

What is API?

API is the acronym for Application Programming Interface. It allows different types of application to communicate with each other by becoming the middleman between any application. For example you can share the song you are listening in Spotify to Twitter with a click of share button. How is that possible? That is because of API.

What is Postman?

Postman is a tool that many testers use to perform API testing . It allows users to perform GET, POST, PUT, DELETE and many other request to web service API.

What is Swagger?

Swagger is an open-source tool that helps developer to document, design, build and consume RESTful web services. In their platform, they have provided a dummy API called Swagger Petstore which I have used in my examples. You will need to visit the website and follow the steps in the upcoming examples in order to follow through this article. You can click on the icon below to visit the dummy Swagger Petstore.

Click for redirection to
Swagger Petstore API

How to find URL In swagger?

  1. Look for the request name for example GET, click to expand it.
  2. Box 1 shows Parameters which contains the variables that needs to be send along the request.
  3. Click the TRY IT OUT button at the place of box 2.
  4. Select the desired parameter and click EXECUTE.
  5. You will see a section called REQUEST URL. That is the URL which we will be using to interact with the API in the examples.

EXPLANATION

URL is the API endpoints which we will be using to send request in Postman. Without having these endpoints, we cannot interact with the API. Developers use Swagger to document the list of all endpoints in their application.
Throughout this article, these are the endpoints you will be using :

GET: https://petstore.swagger.io/v2/pet/findByStatus?status=available
POST: https://petstore.swagger.io/v2/pet/petId
PUT: https://petstore.swagger.io/v2/pet
DELETE: https://petstore.swagger.io/v2/pet/01

Now that you are all set with prerequisite of finding url in swagger, let’s get our hands dirty with Postman!

CREATE A COLLECTION

Steps

  1. At Box 1, click on + NEW COLLECTION.
  2. Fill in a suitable collection NAME.
  3. Fill in a brief DESCRIPTION that reflects the purpose of the collection
  4. Fill up the Authorisation, Pre-request Scripts, Tests, and Variables if applicable.
  5. Click Create to complete the step.

Explanation

collection is like a project folder that we all create at the beginning of any project to store all files in it.
Authorisation tab allows you to store access token that’s usually required to interact with an API. However in this example, it is not required.
Pre-request Scripts allows you to write and execute any scripts that you may need to pass along with your url.
Test allows you to check the response status after performing an API request. It also allows you to execute any scripts that you might want to do after receiving the response from the server side API.
VariableS tab is the place where you might want to declare any variables especially global variable.

Perform a GET Request

Steps

  1. At the top panel, click on the + icon.
  2. At box 1, click on the dropdown and select GET method.
  3. Copy and paste GET /pet/findByStatus url from Swagger Petstore into the address bar.
  4. Hit Send button and wait until you receive the response.
  5. Click Save, and save into the collection we created earlier.

Explanation

Get request is to retrieve all the list of pets that are available in the pet store.
Params, in box 2, is autofilled where its key value pair is set upon copy pasting the url in address bar, which in this example is status = available. In real testing scenario, you can set and loop through any parameters you want to query by storing the variables in VARIABLES tab as mentioned in creating collection above.
RESPONSE STATUS, in box 3, you will see 3 indicators. Status 200 OK means the GET request sent was a success, Time 6.05s was taken to complete the request and size 54.42 KB which indicates the size of the requested data.
RESPONSE BODY, in box 3, contains the response data . This data is usually formatted in JSON or XML format.

Perform a POST Request

Steps

  1. At the top panel, click on the + icon.
  2. Set the HTTP method to POST.
  3. Copy and paste the POST /store/order url from Swagger Petstore into the address bar section.
  4. Under the same POST /store/order, copy the entire body section.
  5. In Postman open the body tab as in Box 2, switch the radio button from none to raw, and paste it into the body section.
  6. In Box 4, set the dropdown to JSON.
  7. Hit the Send button.
  8. Save the file into the collection file.

Explanation

POST request enables us to add a set of data into the server database. Unlike GET request, additionally you will need to send a structured data body with all mandatory tags filled in.
BODY is the tab that allows us to write the data which we want to send. In this example, the attributes tags are id, petId, quantity, shipDate, status and complete. These fields must be filled in with the correct datatype. Any wrong data will result in HTTP 400 error series.

Perform a PUT Request

Steps

  1. At the top panel, click on the + icon.
  2. Set the HTTP method to PUT.
  3. Copy and paste PUT /pet url into the address bar.
  4. Copy the body section and paste it into the Postman body tab as per Box 5.
  5. Modify the dog’s name to any name.
  6. Hit Send button.

Explanation

PUT request is usually invoked to update an existing record in a database. It can also be used to create a record depending on how the API is build. In this example , we have changed the name of a dog in the database.

Perform a DELETE Request

Steps

  1. At the top panel, click on the + icon.
  2. Set the HTTP method to DELETE.
  3. In the previous PUT request example, you would have received an id 1543500600xxxx as part of the return value. This will be your petId. Refer to Box 8 of PUT request example.
  4. Copy and paste DELETE /pet/{petId} url into the address bar.
  5. Replace the petId with the id in step 3.
  6. Hit Send button.

Explanation

Delete request was performed to delete an existing record of a pet in the database. If the delete request was successful, you will receive status 200. Unless the petId is wrong or no longer exist, you will receive status 404.

Summary

What I have demonstrated above are only the basics of RESTful API testing in Postman. It has many more functionalities which are very useful for API testing and can be further extended.

So invest some time to play around with it and you will soon be equipped enough to perform extensive API testing.

I hope you find this article helpful to jumpstart your knowledge in Postman. I will be posting more articles regarding different functionalities of Postman in step by step manner to help you understand more about this tool. Please follow our QA-Synapse blog for upcoming articles. Thank you.

Processing…
Success! You’re on the list.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: