0
0
Postmantesting~5 mins

Response body inspection in Postman

Choose your learning style9 modes available
Introduction

Response body inspection helps you check if the server sends the right data after a request. It ensures your app gets what it expects.

After sending an API request to verify the returned data is correct.
When testing if error messages appear properly in the response.
To confirm that the response format matches the API specification.
When debugging why your app shows wrong or missing information.
To check if sensitive data is not exposed in the response body.
Syntax
Postman
pm.test("Test name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.key).to.eql("expected value");
});

Use pm.response.json() to parse the response body as JSON.

Assertions check if the response data matches what you expect.

Examples
This test checks if the response has a name field equal to "Alice".
Postman
pm.test("Check user name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.name).to.eql("Alice");
});
This test confirms the status field says "success".
Postman
pm.test("Verify status is success", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.status).to.eql("success");
});
This test checks if an error field exists in the response.
Postman
pm.test("Response has error message", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.error).to.exist;
});
Sample Program

This test checks that the response body contains a user object with the correct id and email.

Postman
pm.test("Verify user ID and email", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.user.id).to.eql(123);
    pm.expect(jsonData.user.email).to.eql("user@example.com");
});
OutputSuccess
Important Notes

Always parse the response body before checking its content.

Use clear test names to understand what each test checks.

Check for both existence and exact values to avoid false positives.

Summary

Response body inspection confirms the server sends expected data.

Use pm.response.json() to read JSON responses.

Write clear tests to check specific fields and values.