jwatkins
jwatkins

Reputation: 61

In postman, how do I take a response body and use it in a new request within Tests

I am new to Postman and completely new to Javascript.

I ran a Post request to create a new contract.

Request Body

{
        "progSrvcNm": "009",
        "contractPrtyNm": "PostmanAutomationContract",
        "contractCd": "000",
        "signDt": "2018-01-01",
        "startDt": "2018-01-01",
        "endDt": "2025-01-01",
        "remitTerms": 30
}

and received an ok Response with the new contract number as the response body.

"02974"

I now want to save the response body and use it in a Get request to confirm the data I sent in the Post is what is returned in the get for the new contract.

I attempted to save the variable and use the 'Send Request' snippet in Postman, and when I run I only get a response of another new contract number created.

let newContractNb = pm.response.json();

pm.sendRequest("http://smat-meddev02/MedeaSMATMEDSQL01AICollationFNGAPI2.AffiliateApi/api/Get/" + newContractNb, function (err, response) {
    console.log(response.json());
});

Upvotes: 1

Views: 7084

Answers (2)

jwatkins
jwatkins

Reputation: 61

parsed the data from the json response and saved to a variable as recommended.

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("newContractNb", jsonData);

Then created a GET method using the variable in the URL.

enter image description here

Upvotes: 0

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38502

You can try this way,

on 1st GET request, grab the response body and store the required data to postman environment like postman.setEnvironmentVariable(key, value) more specifically by doing

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("newContractNb", jsonData.newContractNb);

on 2nd GET/POST request, To send the newContractNb, you need to set it as part of the GET/POST request.

Take it as Ref.: http://blog.getpostman.com/2014/01/27/extracting-data-from-responses-and-chaining-requests/

Upvotes: 1

Related Questions