Tip of the Week: Postman - Chaining Requests to Speed Up Manual API Tests
Pass data from response of one API to another API request.
Table of Contents
I was recently playing around with MessageMedia API trying to send SMS and get the status of the SMS sent. Sending the SMS and getting the status of the last sent SMS always happened in succession when testing it manually. Once I send the message, I waited for the API response, grabbed the message id from the response and used that to form the get status request.
Postman is a useful tool if you are building or testing APIs. It allows to create, send and manage API requests.
I added two requests and saved it to a collection in Postman - one to Send Message and other to Get Message status. I have created an environment variable for holding the message id. For the request that sends a message, the below Test snippet is added. It parses the response body of the request and extracts the message id of the last send message. This is then saved to the environment variable. The Test snippet is always run after performing the request.
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable('messageId', jsonData.messages[0].message_id);
tests['Success'] = true;
The Get message request uses the messageId from the environment variables to construct its URL. The URL looks like below.
{% raw %}
https://api.messagemedia.com/v1/messages/{{messageId}}
{% endraw %}
When executing this request, it fetches the messageId from the environment variable, which is set by the previous request. You no longer have to copy message id manually and use it in the URL. This is how we chain the data from one request to another request. Chaining requests is also useful in automated testing using Postman. Hope this helps!
Rahul Nath Newsletter
Join the newsletter to receive the latest updates in your inbox.