How to Check Response Header’s Value in Postman Tests

How to Check Response Header's Value in Postman Tests

21 May 2024 Stephan Petzl Leave a comment Tech-Help

When working with APIs, it’s crucial to verify that the responses from your API calls meet the expected criteria. One common task is to check the value of specific response headers. In this guide, we’ll walk you through how to check the value of a response header in Postman, specifically the “Location” header.

Why Check Response Headers?

Response headers provide important metadata about the response, such as content type, content length, and redirection details. Validating these headers ensures that your API behaves as expected and can be especially critical when dealing with redirects, content negotiation, and caching policies.

How to Check Response Headers in Postman

Postman provides a robust set of tools to automate API testing, including the ability to validate response headers. Here’s a step-by-step guide on how to check the “Location” header in your Postman tests.

Step 1: Basic Header Presence Check

First, let’s ensure that the “Location” header is present in the response.

pm.test("Location header is present", function () {
    pm.response.to.have.header("Location");
});

Step 2: Checking the Value of the “Location” Header

To verify that the “Location” header contains the expected URL, you can use the following code snippet:

pm.test("Redirect location is correct", function () {
    pm.response.to.have.header("Location");
    pm.response.to.be.header("Location", "http://example.com/expected-redirect-url");
});

Step 3: Storing the Header Value for Subsequent Requests

In some scenarios, you might need to use the value of the “Location” header in subsequent requests. You can achieve this by storing the header value in an environment variable:

pm.test("Collect redirect location", function () {
    pm.response.to.have.header("Location");
    var loc = pm.response.headers.get("Location");
    if (loc !== undefined) {
        pm.environment.set("redirURL", loc);
    }
});

Alternative Method Using ES6 Syntax

Postman also supports ES6 syntax, allowing you to write more modern and concise tests. Here’s an example:

pm.test("Redirect location is correct", () => {
    pm.expect(pm.response.headers.get('Location')).to.eql('http://example.com/expected-redirect-url');
});

Conclusion

By following the steps outlined above, you can effectively check and validate response headers in Postman. This practice is essential for ensuring the reliability and correctness of your API responses.

Enhancing Your Testing with Repeato

While Postman is a powerful tool for API testing, integrating automated testing into your mobile applications can be streamlined with tools like Repeato. Repeato is a no-code test automation tool for iOS and Android that helps you create, run, and maintain automated tests for your apps. Its intuitive test recorder and scripting interface make it easy to automate complex use cases quickly.

Repeato’s support for testing websites inside an Android emulator or device, along with its upcoming web testing support, offers a comprehensive solution for your automation needs. To learn more, visit our documentation or check out our blog for the latest updates.

Like this article? there’s more where that came from!