# How can I compare request body and response body?

**URL:** <https://community.postman.com/t/how-can-i-compare-request-body-and-response-body/37418>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [July 21, 2022, 7:42am UTC](https://community.postman.com/t/how-can-i-compare-request-body-and-response-body/37418 "2022-07-21T07:42:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![acihad](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@acihad](https://community.postman.com/u/acihad)\
**Post date:** [July 21, 2022, 7:42am UTC](https://community.postman.com/t/how-can-i-compare-request-body-and-response-body/37418/1 "2022-07-21T07:42:54Z")

</div>

//Is equal to the request body and response body?  
if (pm.response.code === 200) {

```
pm.sendRequest(utils.getApiUrl(pm.request) + pm.response.json(), (error, response) => {
    if (error) {
        console.log(error);
    } else {
        pm.test("Are there all of parameters - ", function () {
            pm.expect(response.json()).is.eql(pm.request.body.raw)
        })
    }
});

```

};

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [July 21, 2022, 10:40am UTC](https://community.postman.com/t/how-can-i-compare-request-body-and-response-body/37418/2 "2022-07-21T10:40:52Z")

</div>

Hi @acihad

You could try something like this;

```auto
const myRequest = pm.request.toJSON().body.raw;
//Remove all characvters and formatting that are not part of the original request.
let myRequestFormatted = myRequest.replace(/[^a-zA-Z0-9":\[\]{},]/g, "");
console.log(myRequestFormatted);

const myResponse = pm.response.json();
console.log(JSON.stringify(myResponse));

if (myRequestFormatted === JSON.stringify(myResponse)){
    console.log("Request and Response MATCH");
}
else{
    console.log("Request and Response DONT MATCH");
}

```

Matched Output;

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

Unmatched Output;

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

---

<div class="post-metadata">

**Author:** ![acihad](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@acihad](https://community.postman.com/u/acihad)\
**Post date:** [July 21, 2022, 2:16pm UTC](https://community.postman.com/t/how-can-i-compare-request-body-and-response-body/37418/3 "2022-07-21T14:16:05Z")

</div>

Hi @w4dd325 ,

Appreciated your answer. I have just carried out your codes but my request did not indicate on the console.

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

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [July 21, 2022, 2:34pm UTC](https://community.postman.com/t/how-can-i-compare-request-body-and-response-body/37418/4 "2022-07-21T14:34:10Z")

</div>

Looking at your screenshot I’d guess thats because your body (raw) is empty.

My code was based on the assumption that the request body was here;

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

---

<div class="post-metadata">

**Author:** ![acihad](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@acihad](https://community.postman.com/u/acihad)\
**Post date:** [July 21, 2022, 3:03pm UTC](https://community.postman.com/t/how-can-i-compare-request-body-and-response-body/37418/5 "2022-07-21T15:03:11Z")

</div>

Hi @w4dd325 ,

Thank you for being so helpful.

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

---

<div class="post-metadata">

**Author:** ![acihad](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@acihad](https://community.postman.com/u/acihad)\
**Post date:** [July 21, 2022, 3:04pm UTC](https://community.postman.com/t/how-can-i-compare-request-body-and-response-body/37418/6 "2022-07-21T15:04:36Z")

</div>

Hi @w4dd325 ,

I have a question about parameter control. How can I check the parameters control whether are there name or description, etc. parameters in request?

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [July 21, 2022, 6:16pm UTC](https://community.postman.com/t/how-can-i-compare-request-body-and-response-body/37418/7 "2022-07-21T18:16:15Z")

</div>

Not sure I understand the question, could you be more specific?

---

<div class="post-metadata">

**Author:** ![acihad](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@acihad](https://community.postman.com/u/acihad)\
**Post date:** [July 22, 2022, 6:30am UTC](https://community.postman.com/t/how-can-i-compare-request-body-and-response-body/37418/8 "2022-07-22T06:30:12Z")

</div>

Hi @w4dd325 ,

How can I check the propery control whether are there name or description, etc. properties in request? For instance, is there name property, and value control of the name property? I would like to carry these controls out separately.

Thank you for your time.

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

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [July 22, 2022, 8:30am UTC](https://community.postman.com/t/how-can-i-compare-request-body-and-response-body/37418/9 "2022-07-22T08:30:42Z")

</div>

I’m not entirely sure what you mean by “property control”

But if you want to check that your objects and properties exist with certain values, you could do something like this;

```auto
const response = pm.response.json();

pm.test("API response contains the expected fields", () => {
  pm.expect(response).to.have.property("id", 0);
  pm.expect(response).to.have.property("name", "doggie");
  pm.expect(response).to.have.property("status", "available");
});

```

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

---

<div class="post-metadata">

**Author:** ![acihad](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@acihad](https://community.postman.com/u/acihad)\
**Post date:** [July 26, 2022, 7:28am UTC](https://community.postman.com/t/how-can-i-compare-request-body-and-response-body/37418/10 "2022-07-26T07:28:57Z")

</div>

hi @w4dd325 ,

I would like to set all of the properties from collection to any request.

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

utils= {  
control: function \_isContains(json, keyname, value) {  
return Object.keys(json).some(key =\> {  
return typeof json[key] === ‘object’ ?  
\_isContains(json[key], keyname, value) : key === keyname && json[key] === value;  
});  
}

};

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

//Is equal to the request body and response body?  
var expect = pm.variables.get(“keys”)

pm.test(“Are there all of parameters”, function() {  
pm.expect(pm.response.json()).to.have.keys(expect);

});
