Regular expression over different sets of API response Verification

Hi Team,

I just want to verify my API response with one line of code(script) only.

As of now my API response is inconsistent, and response gonna be either

OR
“{}”

What gonna be best way to verify those responses with one line code only?

As of now my script is like:
pm.response.to.have.body("");
pm.response.to.have.body("{}");

I was going through chai assertion “anyof” but got to know it will work with numbers only.

Note: If response looks like below then
{“recentOrders”:};
who one line of code will be?

Why do you want only one line of code? That is an odd requirement.

What I find to be the most accurate is to load the response body from your API definition document and use ajv to validate the schema. That takes more than one line, though.

Reason behind is, I am not sure what response gonna be. Few times its says as “” and few times as “{}”.
So I want to make a script so that in both of the cases will work, No failure.

So looking for regular expression where something we can do.

pm.expect(pm.response.json()).to.satisfy(function (status) {
 if (_.isEqual(status,{}) || _.isEqual(status,[])) {
           return true;
       } else {
           return false;
       }
    },"Expected response to be [] or {} but got "+pm.response.json());

May I know how exactly work this?
Anyway tried this way and worked fine.
pm.test(“data verification”, function () {

let jsonData = pm.response.json();

pm.expect(jsonData).to.eql(regular(jsonData));

});

function regular(value) {

if (value === “{}”) {

value = {};

} else {

value= ;

}

return value;

}

you can use as it is what is the error you are getting,

is the response ? or is its {“recentOrders”:}

If any additional result, will add if else conditions there.

May I know how exactly we can use above mentioned method?

its the same approach you used , satisfy checks if the result satisfies the function . Instead of using equals you can use the function you created in satisfy also

1 Like