Joseph Michael Pesch
VP Programming

Postman Collection Runner Save Response Body to Environment Variable

by 9. August 2019 08:14

First, create an environment variable called responseData, with the value [].

Then, add the following code under 'Tests' in Builder. Postman environment variables are intended to be used as string, so we will parse the object then push the JSON responses from the API into the object array.

var jsonData = JSON.parse(responseBody); var old = pm.environment.get("responseData"); old = JSON.parse(old); // filter jsonData if needed

old.push(jsonData); old = JSON.stringify(old); pm.environment.set("responseData", old); console.log(pm.environment.get("responseData")); Now you can retrieve a nested JSON object with all the response data included by viewing the environment variables value (see example below).

Warning: you must reset the responseData value as [] after every use of Collection Runner in order to avoid keeping data from previous runs.

Tags: