1

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());
});
jwatkins
  • 61
  • 2
  • 8

2 Answers2

1

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/

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
  • I guess i'm confused. I did the first part and extracted the response to create the variable. Is the second part using the variable in a separate GEt request? Is it not possible to run another request from the Tests Section of this Post request? – jwatkins Nov 28 '18 at 16:00
  • now I am getting confused what is your requirement :( – A l w a y s S u n n y Nov 28 '18 at 16:11
  • I'm sorry. I wanted to run the post method to create the new contract and obtain the Number as a response. I then wanted to run a Get for the new contract number to confirm the data matched what I sent in the previous request body. I was thinking I could do all of this within the Tests section of the Post method via the 'send a request' snippet, but I don't know much so that may not be possible, or the best way. – jwatkins Nov 28 '18 at 16:29
  • The solution Curious_mind worked, except instead of saving the variable to from data, I used it in the URL of a Get method. – jwatkins Nov 28 '18 at 18:14
  • @jwatkins glad it helps you somehow, best of luck :) – A l w a y s S u n n y Nov 29 '18 at 02:28
0

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

jwatkins
  • 61
  • 2
  • 8