In API testing, API chaining is like arranging steps in a dance. It’s about doing many API requests in a row, where each one depends on the last.
Picture this: you have an API to fetch user details and another to fetch posts for each user. Now, with API chaining, you start by gathering user details, then gracefully pluck out their IDs from the response, and finally, glide into fetching posts for each user, one by one. It’s a synchronised flow of data, ensuring that each move sets the stage for the next, ultimately delivering a flawless performance.
This can be achieved using Postman scripts, specifically through the use of environment variables and Postman’s scripting capabilities.
Example with Scenario:
Here’s an example of how to achieve API chaining in Postman using scripts:
Suppose we have two API endpoints:
- Endpoint to retrieve user details: GET /api/users
- Endpoint to retrieve user’s posts: GET /api/posts/{userId}
We want to first retrieve the list of users and then for each user, retrieve their posts.
- First, send a request to retrieve the list of users:
- Method: GET
- URL: https://<userYourApi>/api/users
- In the Tests tab of this request, add the following script to extract user IDs from the response and store them in an environment variable:
let users = pm.response.json(); let userIds = users.map(user => user.id); pm.environment.set(“userIds”, JSON.stringify(userIds)); |
Let’s Understand what each line does:
let users = pm.response.json();
- This line retrieves the response data from the API request made in Postman and parses it as JSON format. The parsed data is then stored in the variable users.
- let userIds = users.map(user => user.id);
- Here, the map() function is used on the users array to extract the id property of each user object. This creates a new array called userIds containing only the user IDs.
- pm.environment.set(“userIds”, JSON.stringify(userIds));
- Finally, this line sets the value of an environment variable named “userIds” in Postman’s environment. The value assigned to this variable is the JSON string representation of the userIds array. This allows the user IDs to be stored and accessed within the environment for later use in subsequent requests.
rIds));
- Now, we’ll use the extracted user IDs to make subsequent requests to retrieve each user’s posts. Create a new request and set the URL as follows:
- Method: GET
- URL: https://<userYourApi>/api/posts/{{userId}}
In the Pre-request Script tab of this request, add the following script to dynamically set the user ID for each request:
let userIds = JSON.parse(pm.environment.get(“userIds”)); let userId = userIds.shift(); pm.environment.set(“userId”, userId); |
Let’s go through each line:
- let userIds = JSON.parse(pm.environment.get(“userIds”));
- This line retrieves the value of the environment variable “userIds” using pm.environment.get(“userIds”), which returns a string representing a JSON array. JSON.parse() then parses this string into a JavaScript array and assigns it to the variable userIds.
- let userId = userIds.shift();
- The shift() method is called on the userIds array, which removes and returns the first element of the array. This element (the user ID) is then assigned to the variable userId.
- pm.environment.set(“userId”, userId);
- Finally, the value of the userId variable is set as the value of the environment variable “userId” using pm.environment.set(). This allows the user ID to be stored and accessed within the environment for later use in subsequent requests.
erId)
- In the Tests tab of the same request, add the following script to remove the processed user ID and set the next user ID for the subsequent request:
let userId = pm.environment.get(“userId”); pm.environment.set(“userId”, userIds.shift()); |
Let’s break down each line:
- let userId = pm.environment.get(“userId”);
- This line retrieves the value of the environment variable “userId” using pm.environment.get(“userId”) and assigns it to the variable userId. The pm.environment.get() function is used to access environment variables within Postman.
- pm.environment.set(“userId”, userIds.shift());
- Here, userIds.shift() is called on the userIds array, which removes and returns the first element of the array. This element (the user ID) is then set as the value of the environment variable “userId” using pm.environment.set(). This allows the updated user ID to be stored in the environment for later use in subsequent requests.
- Now, repeat steps 3-5 for as many requests as needed to process each user’s posts.
By following these steps and scripts, you can achieve API chaining in Postman, where the output of one request serves as input for subsequent requests, allowing you to chain multiple API calls together seamlessly.
Conclusion:
In conclusion, API chaining in Postman facilitates sequential data processing by linking multiple API requests. By fetching user data and extracting IDs for subsequent requests, testers can seamlessly retrieve and process information step by step. This method enables efficient and organised data handling, enhancing the overall testing process in Postman.
The beauty of API chaining lies in its ability to mimic real-world scenarios with precision. By linking multiple API requests together, we create a dynamic chain of events that mirrors how users interact with our applications. This not only helps us validate the functionality of interconnected APIs but also paints a vivid picture of how our system performs in the wild. From fetching user profiles to retrieving their latest posts, API chaining enables us to choreograph these interactions effortlessly, ensuring that our APIs dance to the rhythm of user expectations.