Vikas Kumar
Vikas Kumar

Reputation: 165

How to check unwanted parameters in response body in Postman?

Refer to the below response body as an example, the only valid parameters of the response body are StatusCode and Message. We can add assertions and easily check if the fields are present with the correct value or not.

Now, I want to check if any unwanted parameters are being populated in the response and make the correction in this case. Is there any way to check this in postman using test assertions?

{
    "statusCode": 200,
    "message": "Here is the detail message",
    "data": "Unwanted parameter"
    "UnwantedLink": "www.unwantedlink.com"

}

Upvotes: 0

Views: 712

Answers (2)

PDHide
PDHide

Reputation: 19929

use json schema validation :

you can use https://www.jsonschema.net/home to generate schema , click setting and settings and set options like:

enter image description here

schema = {

"type": "object",
"properties": {
    "statusCode": { "type": "integer" },
    "message": { "type": "string" }
},
"additionalProperties": false
};



pm.response.to.have.jsonSchema(schema)

Upvotes: 1

lucas-nguyen-17
lucas-nguyen-17

Reputation: 5917

You can check keys using chaijs

const res = pm.response.json();

pm.test("check keys in response", () => {
    pm.expect(res).have.all.keys("statusCode", "message");
})

Example 2 cases below

enter image description here

Upvotes: 1

Related Questions