How to validate response when it fails and passes?

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

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

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");
});

}

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

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);
 });
}

2 Likes

Thanks a lot @danny-dainton

2 Likes