How to append a request body (Saved to a environment variable from previous response)

Hi Folks,
Hope you all are doing great…

I have a situation where i need to capture the response from previous request and save it to an environment variable and append it with some fixed json code and then use it as a request payload - body in the next request.

Request 1 - I am capturing the response and saving it to an environment variable

var jsonResponse = pm.response.json(); var getJsonResponse = JSON.stringify(jsonData);

pm.environment.set(“details”, getJsonResponse);

Below is the response captured and assigned to the above variable:

{

“programNumber”: “xxxxxxxxxxxxx”,

“wpType”: “xxx”,

“referralType”: “xxxxxxxxx”,

“status”: “xxx”,

“openDate”: “2017-04-27”,

“transitional”: false,

“benefitAmount”: 357.0,

“requirementsMet”: false,

“reopenInd”: false,

“userModel”: {

“offices”: ,

“roles”:

}

}

Now i want to append the below JSON part to the response saved to the environment variable and send it as a request body for next request

,

"$promise": {},

"$resolved": true,

"staffId": “xxxxxx-18cf-xxxx-b18d-xxxxxxxxxx”,

"transitionalDate": null

I have the answer for my question posted here.

We can use Object.assign to merge two objects in JavaScript

const postmanResponse = { name: “xxxxx”, age: 22 };

const appendJsonCode = { dob: 05/05/1990, pin: 123456 };

const newResponse = Object.assign(response, additional);
pm.environment.set(“nextRequestBody”, JSON.stringify(newResponse));

Thank you

1 Like