# How to validate response when it fails and passes?

**URL:** <https://community.postman.com/t/how-to-validate-response-when-it-fails-and-passes/25027>\
**Category:** Help Hub\
**Created:** [June 18, 2021, 8:17pm UTC](https://community.postman.com/t/how-to-validate-response-when-it-fails-and-passes/25027 "2021-06-18T20:17:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ps198478320](https://avatars.discourse-cdn.com/v4/letter/p/5fc32e/32.png) [@ps198478320](https://community.postman.com/u/ps198478320)\
**Post date:** [June 18, 2021, 8:17pm UTC](https://community.postman.com/t/how-to-validate-response-when-it-fails-and-passes/25027/1 "2021-06-18T20:17:22Z")

</div>

Hi,  
i have a request which returns 200 if everything is fine.  
i validate with this code:

> pm.test(“Status code is 200”, function () {  
> pm.response.to.have.status(200);  
> });

When test fails sometime i receive this response in json:

> {  
> “code”: “D500”,  
> “message”: “MSG: Error api.”,  
> “httpStatus”: 500  
> }

How can i write script to cover the test when it passes or fails in the same script?

Thank you

---

<div class="post-metadata">

**Author:** ![ps198478320](https://avatars.discourse-cdn.com/v4/letter/p/5fc32e/32.png) [@ps198478320](https://community.postman.com/u/ps198478320)\
**Post date:** [June 18, 2021, 8:38pm UTC](https://community.postman.com/t/how-to-validate-response-when-it-fails-and-passes/25027/2 "2021-06-18T20:38:28Z")

</div>

It works with this but is it correct to do this?

```auto
if (pm.response.to.have.status(200)){
console.log ("204 was returned");
} else { 
console.log("no id ...");

pm.test("Check the error message", () => { pm.expect(pm.jsonData.code).to.eql("D500"); pm.expect(pm.jsonData.message).to.eql("MSG: Error api.");
 pm.expect(pm.jsonData.httpStatus).to.eql("500");
});

}

```

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [June 19, 2021, 8:33am UTC](https://community.postman.com/t/how-to-validate-response-when-it-fails-and-passes/25027/3 "2021-06-19T08:33:48Z")

</div>

It’s one way of doing it but your syntax didn’t look quite right:

```auto
let jsonData = pm.response.json();

if(pm.response.code === 200){
    console.log ("200 was returned");
} 
else { 
 console.log("no id ...");
 pm.test("Check the error message", () => {   
    pm.expect(jsonData.code).to.eql("D500");
    pm.expect(jsonData.message).to.eql("MSG: Error api."); 
    pm.expect(jsonData.httpStatus).to.eql(500);
 });
}

```

---

<div class="post-metadata">

**Author:** ![ps198478320](https://avatars.discourse-cdn.com/v4/letter/p/5fc32e/32.png) [@ps198478320](https://community.postman.com/u/ps198478320)\
**Post date:** [June 19, 2021, 6:51pm UTC](https://community.postman.com/t/how-to-validate-response-when-it-fails-and-passes/25027/4 "2021-06-19T18:51:13Z")

</div>

Thanks a lot @danny-dainton
