API Request and Response chaining using Postman

26 / Jun / 2022 by rahul.upadhyay 0 comments

What is an API?

API stands for Application Programming Interface which works as an intermediary between two applications to serve the request and responses.

APIs are developed to speed up the functionality and request response mechanism for an applications.It allows to add functionality from other providers to the existing system, it can be different sources of information, verification services or communication services.

Basically, API essentially is a component that enables communication between two different applications, these applications could be developed differently like OS used can be different, Tech Stack can be different, DB used can be different.

The API allows the developers to make a call/request in order to send and receive some information from a server. The API uses JavaScript Object Notation to communicate over HTTP i.e. Hyper Text Transfer Protocol. HTTP requests are sent by client machines to get or receive some information from the server.It has four main elements namely – An HTTP method, Request type like GET, PUT, POST etc, Request HEAD/OPTIONS and Request Body.

These are the basic types of HTTP request-

1. GET
2. POST
3. PUT
4. DELETE

In this blog, we are going to learn the Request/Response Chaining using one of the popular API testing tools i.e. POSTMAN-

Problem Statement-

While testing any API, we use to face a problem where we need to provide the Response of an API as the Request Parameter for another API.

More precisely, We will learn to get a response from one API and pass it as request parameter in another API.

In the current example, I am using 2 APIs to chain their request and responses. The first API allows to upload a file and on the basis of that it generates a unique work-order id. The second API uses the work order id to check the status of the uploaded document. We would need to follow the following steps to achieve the same-

Step 1:

Go to Postman and Click on New -> Collection -> Add name to your collection say API_Chaining

Step 2:

Create/Add the first API requests which you want to chain and save it, it will give the response in the following format –

{
“MSG”: “Workorder Created”,
“WORKORDER_ID”: “12345678909876”
}

Step 3:

Use Environment variable to parameterised the response value of API 1.

To do so, Go to New – > Environment and Provide any environment name

Add a new Variable name say ‘Dynamic_WorkOrderID’ and provide some initial value like abc.

Select the newly created environment from the top right corner.

Step 4:

Now Add the Test Script to set the value of WorkOrder ID in ‘Dynamic_WorkOrderID’ variable

Go to Test tab and add the script to set the value of work order id dynamically-

bodyValue = JSON.parse(responseBody)

bodyData = bodyValue.WORKORDER_ID

console.log(bodyData)

pm.environment.set(“Dynamic_WorkOrderID”, bodyData);

The first line bodyValue = JSON.parse(responseBody), will fetch value from the response body of API 1

Now we would need to provide the JSON path of the variable whose value we want to set in the environment variable dynamically-

So, The second line is setting the value of WORKORDER_ID in bodyData variable

bodyData = bodyValue.WORKORDER_ID

This statement simply prints the value which is set under bodyData variable

console.log(bodyData)

Now,We would need to set the value of bodyData variable in the environment variable i.e. Dynamic_WorkOrderID.

pm.environment.set(“Dynamic_WorkOrderID”, bodyData);

Step 5:

Now go to API 2 and set the body param as below –

{
“workorder_id”: “{{Dynamic_WorkOrderID}}
}

This will set the value of body parameter ‘workorder_id’ as per the value of environment variable value and the user need not to set it manually again and again to check the status for different work order ids and the user can simply hit the send request button in order to get the response from that API.

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *