0
0
Postmantesting~15 mins

Request headers in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify custom request headers are sent and received correctly
Preconditions (2)
Step 1: Open Postman and create a new GET request
Step 2: Set the request URL to https://postman-echo.com/headers
Step 3: Add a custom header with key 'X-Test-Header' and value 'TestValue123'
Step 4: Send the request
Step 5: Observe the response body for the headers returned
✅ Expected Result: The response body contains the header 'X-Test-Header' with value 'TestValue123'
Automation Requirements - Postman/Newman
Assertions Needed:
Verify response status code is 200
Verify response JSON contains 'X-Test-Header' with value 'TestValue123'
Best Practices:
Use Postman test scripts to assert response values
Use environment variables for header values if needed
Keep tests independent and idempotent
Automated Solution
Postman
pm.test('Status code is 200', function () {
    pm.response.to.have.status(200);
});

pm.test('Response contains custom header', function () {
    const jsonData = pm.response.json();
    pm.expect(jsonData.headers['x-test-header']).to.eql('TestValue123');
});

The first test checks that the HTTP status code returned is 200, meaning the request was successful.

The second test parses the JSON response body and verifies that the custom header 'X-Test-Header' is present with the exact value 'TestValue123'.

This ensures the header was sent and received correctly by the API.

Common Mistakes - 3 Pitfalls
{'mistake': 'Not setting the header key with correct casing', 'why_bad': 'Headers are case-insensitive but the API might return them in lowercase, causing assertion failures if case is mismatched.', 'correct_approach': "Use lowercase keys when accessing headers in response JSON, e.g., 'x-test-header'."}
Not checking the response status code before asserting header values
Hardcoding header values in multiple places
Bonus Challenge

Now add data-driven testing with 3 different custom header values

Show Hint