# How to verify full body response within a Pre requisite script and compare with response body?

**URL:** <https://community.postman.com/t/how-to-verify-full-body-response-within-a-pre-requisite-script-and-compare-with-response-body/15123>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [August 18, 2020, 7:18pm UTC](https://community.postman.com/t/how-to-verify-full-body-response-within-a-pre-requisite-script-and-compare-with-response-body/15123 "2020-08-18T19:18:24Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [August 18, 2020, 7:18pm UTC](https://community.postman.com/t/how-to-verify-full-body-response-within-a-pre-requisite-script-and-compare-with-response-body/15123/1 "2020-08-18T19:18:24Z")

</div>

Hi ,  
I have 67 branches below and i have added a variable within  
Pre - Requisite script  
var expectedJsonBody =

[

```
{

    "siteId": 685,

    "siteName": "Saint Francis Hospital-Memphis",

    "hcoid": 2659,

    "defaultSite": false,

    "address": {

        "address1": "5959 Park Avenue",

        "address2": null,

        "city": "Memphis",

        "state": "TN",

        "zip": "38119-5198"

    }

},

{

```

pm.environment.set(“ExpectedSitesTodisplay”, JSON.stringify(expectedJsonBody));

and in the test of the script i have  
pm.test(“Verify Expected Results sites match the Actual Results of all sites for Darlene”), function(){

pm.expect(savedData.response).to.equal(“ExpectedSitesTodisplay”);

};

it is showing up as pass, but when i enter  
ExpectedSitesTodisplay variable within the console, its saying undefined,  
and if i change something in the pre requisite script and click send it should fail but it still passes…

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/b/8/b82aef25e31ab34e52f0c76815386783effc3503.png)

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [August 20, 2020, 1:58pm UTC](https://community.postman.com/t/how-to-verify-full-body-response-within-a-pre-requisite-script-and-compare-with-response-body/15123/2 "2020-08-20T13:58:58Z")

</div>

Your test isn’t testing the variable you set in your prerequest script.

```auto
pm.test(“Verify Expected Results sites match the Actual Results of all sites for Darlene”), function(){
  const expectedSitesToDisplay = pm.environment.get('ExpectedSitesTodisplay');  
  pm.expect(savedData.response).to.equal(expectedSitesToDisplay);
};

```

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [August 20, 2020, 5:05pm UTC](https://community.postman.com/t/how-to-verify-full-body-response-within-a-pre-requisite-script-and-compare-with-response-body/15123/3 "2020-08-20T17:05:47Z")

</div>

Hi Thanks a lot ,

I did try this and am getting it to pass, but lets say if i change one parameter or letter in the pre requisite script it should fail but its not failing … so meaning its not comparing actual and expected.

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/d/df81540de9b5c31c76b0f8cbb45f65198c859bb5.png)

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [August 20, 2020, 5:19pm UTC](https://community.postman.com/t/how-to-verify-full-body-response-within-a-pre-requisite-script-and-compare-with-response-body/15123/4 "2020-08-20T17:19:43Z")

</div>

You still aren’t using the variable in your test.

You need to take off the quotes around the variable name

```auto
pm.expect(savedData.response).to.equal(ExpectedSitesTodisplay);

```

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [August 20, 2020, 6:47pm UTC](https://community.postman.com/t/how-to-verify-full-body-response-within-a-pre-requisite-script-and-compare-with-response-body/15123/5 "2020-08-20T18:47:34Z")

</div>

Hi, I did try that …  
ill break it down maybe its something in the pre requisite script that should not be there:

Pre requisite script

var expectedJsonBody =  
[

```
{

    "siteId": 685,

    "siteName": "Saint Francis Hospital-Memphis",

    "hcoid": 2659,

    "defaultSite": false,

    "address": {

        "address1": "5959 Park Avenue",

        "address2": null,

        "city": "Memphis",

        "state": "TN",

        "zip": "38119-5198"

    }

},

{

```

// pm.environment.get(“expectedJsonBody”);

pm.environment.set(“ExpectedSitesTodisplay”, JSON.stringify(expectedJsonBody));

in Test of other script:  
let response = pm.response.json();

savedData = JSON.stringify(response);

pm.environment.set(“savedData”, savedData);

var jsonData = JSON.parse(savedData)

pm.test(“Verify Expected Results sites match the Actual Results of all sites for Darlene”), function(){

const ExpectedSitesTodisplay = pm.environment.get(‘ExpectedSitesTodisplay’);

pm.expect(savedData.response).to.equal(ExpectedSitesTodisplay);

};

Its passing but if i change the value in the pre req script , it does not fail it still passes…

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [August 20, 2020, 7:01pm UTC](https://community.postman.com/t/how-to-verify-full-body-response-within-a-pre-requisite-script-and-compare-with-response-body/15123/6 "2020-08-20T19:01:26Z")

</div>

I’m curious what you’re doing with the savedData variable. Is that the response of the request you are testing?

Or is it the response of a different test? I’m not sure why you’re setting it in an environment variable.

Maybe some screenshots of what this looks like in Postman would help.

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [August 20, 2020, 9:13pm UTC](https://community.postman.com/t/how-to-verify-full-body-response-within-a-pre-requisite-script-and-compare-with-response-body/15123/7 "2020-08-20T21:13:20Z")

</div>

Sure,

I was trying to get the full response body through the SavedData (actual response body) and compare it to the pre requisite variable (ExpectedSitesTodisplay) (expected) (pre requisite)

when i do this it passes, but if i change anything on the pre requsite script it still passes which it should not.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/b/7/b7498c2efa6f7554ce9784fe8b0347631b6f1029.png)

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/c/2/c2382f6957d32712632d27b1d56b0d36d848da11.png)

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/4/e/4ef8c77228bafc71eb9754e53d07c4c64341e127.png)  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/9/b/9b1dabb61b275db171ca66578917eefa4074c449.png)

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [August 20, 2020, 9:14pm UTC](https://community.postman.com/t/how-to-verify-full-body-response-within-a-pre-requisite-script-and-compare-with-response-body/15123/8 "2020-08-20T21:14:36Z")

</div>

I changed the saint to faint and it still passed

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/a/8/a863d398c13e6dbbc5e96b3ffd842995a23a92b9.png)

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [August 20, 2020, 11:01pm UTC](https://community.postman.com/t/how-to-verify-full-body-response-within-a-pre-requisite-script-and-compare-with-response-body/15123/9 "2020-08-20T23:01:00Z")

</div>

I found this from this forum

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/b/3/b3d37eb51184a78580f1a12027e844f2ac83461b.png)

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [August 21, 2020, 11:53am UTC](https://community.postman.com/t/how-to-verify-full-body-response-within-a-pre-requisite-script-and-compare-with-response-body/15123/10 "2020-08-21T11:53:17Z")

</div>

Did that solve your problem?

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [August 21, 2020, 1:32pm UTC](https://community.postman.com/t/how-to-verify-full-body-response-within-a-pre-requisite-script-and-compare-with-response-body/15123/11 "2020-08-21T13:32:39Z")

</div>

hi Allen,

no it did not the part where i compare the response body with the variable is still not working …  
i tried the code i wrote but still isn’t working.

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [August 21, 2020, 1:57pm UTC](https://community.postman.com/t/how-to-verify-full-body-response-within-a-pre-requisite-script-and-compare-with-response-body/15123/12 "2020-08-21T13:57:03Z")

</div>

I’m clearly missing something. What are you trying to do in a pre-request script? All your screenshots show test scripts.

How many requests are you running? 1, 2, 3…?

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [August 21, 2020, 2:07pm UTC](https://community.postman.com/t/how-to-verify-full-body-response-within-a-pre-requisite-script-and-compare-with-response-body/15123/13 "2020-08-21T14:07:27Z")

</div>

![image](https://us1.discourse-cdn.com/getpostman/original/3X/1/1/11794527acfc8432d45dcdec6982b7fd90e915f9.png)

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/0/6/063c0722038bffa2c1a4a3277dabad512bfd0b6a.png)

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [August 25, 2020, 10:52pm UTC](https://community.postman.com/t/how-to-verify-full-body-response-within-a-pre-requisite-script-and-compare-with-response-body/15123/14 "2020-08-25T22:52:02Z")

</div>

Hi, for the pre requesite script i have copied the full response body … and pasted it within the pre requisite script and then i want to verify the pre requisite script response body matches the response body within the get Request .

Just one GET request (Actual) , Pre req of that get Request (Expected) …

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [August 26, 2020, 11:33am UTC](https://community.postman.com/t/how-to-verify-full-body-response-within-a-pre-requisite-script-and-compare-with-response-body/15123/15 "2020-08-26T11:33:45Z")

</div>

Ok I think I’m starting to get it now.

So you are running this test to make sure the schema and values don’t change over time?

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [August 26, 2020, 1:12pm UTC](https://community.postman.com/t/how-to-verify-full-body-response-within-a-pre-requisite-script-and-compare-with-response-body/15123/16 "2020-08-26T13:12:20Z")

</div>

Hi, Yes that is correct… i already tested to see if the response body is correct today , i want to ensure a week from now i am getting the same …

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [August 26, 2020, 1:29pm UTC](https://community.postman.com/t/how-to-verify-full-body-response-within-a-pre-requisite-script-and-compare-with-response-body/15123/17 "2020-08-26T13:29:15Z")

</div>

Then you should not be doing this in a pre-request script. You should be setting the variable in the test scripts which run AFTER the request is executed.

Are you wanting to validate only the schema or do you also want to validate the data for this specific request?

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [August 28, 2020, 9:59pm UTC](https://community.postman.com/t/how-to-verify-full-body-response-within-a-pre-requisite-script-and-compare-with-response-body/15123/18 "2020-08-28T21:59:13Z")

</div>

Hi not the schema too much more the data all of the data … how would i go about doing this …?

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [August 31, 2020, 11:36am UTC](https://community.postman.com/t/how-to-verify-full-body-response-within-a-pre-requisite-script-and-compare-with-response-body/15123/19 "2020-08-31T11:36:46Z")

</div>

Personally, I would save off the values I want to check in the **tests** tab.

Then the next time the collection runs, you can compare the current values with the old values, and save off the new ones as well.

---

<div class="post-metadata">

**Author:** ![michal.robaszewski.j](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michal.robaszewski.j/32/5378_2.png) [@michal.robaszewski.j](https://community.postman.com/u/michal.robaszewski.j)\
**Post date:** [September 7, 2020, 8:11am UTC](https://community.postman.com/t/how-to-verify-full-body-response-within-a-pre-requisite-script-and-compare-with-response-body/15123/20 "2020-09-07T08:11:13Z")

</div>

I added comments to parts of this snippet that you have provided.

> [@shahbaz.shiekhqa](#):
>
> in Test of other script:  
> let response = pm.response.json(); // Here you already have json format to validate with variable from pre-request script
> 
> savedData = JSON.stringify(response);
> 
> pm.environment.set(“savedData”, savedData); // if you need it later
> 
> var jsonData = JSON.parse(savedData) //unused
> 
> pm.test(“Verify Expected Results sites match the Actual Results of all sites for Darlene”), function(){
> 
> const ExpectedSitesTodisplay = pm.environment.get(‘ExpectedSitesTodisplay’);
> 
> pm.expect(savedData.response).to.equal(ExpectedSitesTodisplay);
> 
> };

change last line to  
pm.expect(response).to.deep.equal(ExpectedSitesTodisplay);  
or  
pm.expect(response).to.deep.include(ExpectedSitesTodisplay);

Postman use the CHAI assertion library - [Expect / Should - Chai](https://www.chaijs.com/api/bdd/#method_deep)  
Read the examples there and this should help.

If you need to check also data by hand I can share this script

**Save test function as variable**  
Save in Collection, Directory, folder some other test

```auto
pm.globals.set('helper', function loadhelper() {

 let helper = {};

helper.test = (model, response) => {
// you can manually provide nested object instead of whole response. 
        if(response == null)
            response = pm.response.json()

        for (var x in model) { //It's list of keys of model json - the first level. any nested objects are check deeply. 
// the X is single field from model to be validated. . You can do it from response to model. I prefer to check that way. As response may have audit fields like created by created date and other 
            try {

                if(model[x] == null){

                    pm.test("Model null in respons - " + x, function () {

                        pm.expect(response[x]).to.be.null;  

                    });                     

                } else if (x.includes("date") || x.includes("Date")) {
// dates must be in same format to be compared 
                    let res = new Date(response[x]).toISOString(); 

                    let mod = new Date(model[x]).toISOString();

                    pm.test("Model match response - date of " + x, function () {

                        pm.expect(mod).to.equal(res);

                    });

                } else if (x.includes("id") || x.includes("Id")) {
                    //for debug if failed

                    // console.log("id " + x ) 

                    // console.log("id slice " + x.slice(0,-2) )
                   // if you have nested response 
                    if (x === "OtherUnitId") { // If Model and respons name does not equal but data should be checked. This is for exceptions that are constant rather than unexpected

                        pm.test("Model match response - Id of object " + x, function () {
                            // ParseInt is necessary if you run test from newman. There everything is converted to string.... don't ask why ... but parse is necessary   
                            pm.expect(parseInt(model[x])).to.equal(response["thatUnit"].id);

                        });

                    // if you have flat response
                    } else {

                        pm.test("Model match respons - Id of " + x, function () {

                            pm.expect(parseInt(model[x])).to.equal(parseInt(response[x]));

                        });

                    } else { // this 'else' or above must be deleted. 
                   // any other nested 'ID check' field if you have flat response remove the slice and '.id'
                        pm.test("Model match respons - Id of " + x, function () {

                            pm.expect(parseInt(model[x])).to.equal(response[x.slice(0, -2)].id);

                        });

                    }

                }

                else if (typeof(model[x]) == typeof({}) && model[x] != null ){                    

                    pm.test("Model deep match respons - Field " + x, function (){                        

                        pm.expect(model[x]).to.deep.include(response[x]);

                    });
                    // here you can call recursion to this method and check deeply nested object. But it's danger zone :) 

                }

                else { // any other field, as strings.... mostly stings 

                    pm.test("Model match respons - Field " + x, function () {

                        pm.expect(model[x]).to.equal(response[x]);

                    });

                }                

            }

            catch (e) { // debugging purpose 

                pm.test("ERR No such field in response " + x, function () {

                    pm.expect.fail("Err:" + e);

                });

            }

        }

    }

 return helper;

} + '; loadhelper();');

```

Usage in test

```auto
var model = JSON.parse(pm.collectionVariables.get("XYZ_Model"));

// Use helper function from variables 

const helper = eval(pm.globals.get("helper"));

pm.test("Validate Model", helper.test(model));
pm.test("Validate Model", helper.test(model.nestedResource, pm.response.json().nestedResource)); // instead of recursion in main helper function as it is danger zone. 

```

Have a nice day 🙂

[Next page](https://community.postman.com/t/how-to-verify-full-body-response-within-a-pre-requisite-script-and-compare-with-response-body/15123.md?page=2)
