Monday, August 5, 2019

How to Retrieve Response Data and store it in variables?

How to Retrieve Response Data and store it in variables?

While working with Postman, sometimes we come across some situations where we need to use the Response data of say Request 1 as an input to Request 2 and so on. This process of reusing the data in other requests is called as Chaining.

To get started with Chaining. first thing to know is how to store the re-usable response data in variables.

Have a look at the screenshot below:

In the above screenshot, I have used a dummy API request for testing purpose. Here while hitting "/posts" we will get all the posts data as a response for the request we have made.

Now, suppose we have an another API where we need to pass a particular posts "id" as an input to get the details of the same. Let's see how to achieve this.



Here, we have written some tests in "Test Scripts" tab. Let's see what these lines of code is doing:

pm.environment.clear() - This clears all the environment variables of the specific environment say we are using "QA" environment, so this line of code will clear all the variables from the "QA" environment.

var response = JSON.parse(responseBody) - Here, we are declaring a variable name "response" and we're storing the response of the API in this variable.

pm.environment.set("id", response[0].id) - This line of code is setting an environment variable with name i.e variable key as "id" and as the response is in the form of array, we're getting the post id from the "0th" index and storing in the id variable here.

No comments:

Post a Comment

Featured Post

API Automation using Newman - Part II

HTML Report via Newman In previous post we have seen how to setup Newman and execute the collections via Newman in command line. Ne...