0
0
Postmantesting~5 mins

Status codes reading in Postman

Choose your learning style9 modes available
Introduction

Status codes tell us if a web request worked or not. They help us understand the result quickly.

Checking if a website page loads successfully.
Verifying if a login API accepted the username and password.
Confirming if data was saved correctly after sending it to a server.
Detecting if a requested resource was not found.
Testing if a server is down or unreachable.
Syntax
Postman
pm.response.code

This code gets the numeric status code from the response.

Common codes: 200 means OK, 404 means Not Found, 500 means Server Error.

Examples
This test checks if the response status code is exactly 200 (OK).
Postman
pm.test("Status code is 200", () => {
    pm.response.to.have.status(200);
});
This code prints a message if the status code is 404 (Not Found).
Postman
if(pm.response.code === 404) {
    console.log("Page not found");
}
This test checks if the status code is any success code between 200 and 299.
Postman
pm.test("Status code is success", () => {
    pm.expect(pm.response.code).to.be.within(200, 299);
});
Sample Program

This Postman test script checks if the response status is 200. It also logs a message if the status is 404.

Postman
pm.test("Check if response status is 200", () => {
    pm.response.to.have.status(200);
});

pm.test("Log message for 404 status", () => {
    if(pm.response.code === 404) {
        console.log("Resource not found");
    }
});
OutputSuccess
Important Notes

Always check status codes before reading response data.

Use status codes to decide next test steps or error handling.

Summary

Status codes show if a request worked or failed.

Use pm.response.code to read the status code in Postman.

Write tests to check for expected status codes.