Response headers tell us important details about the server's reply to our request. They help us understand how the server handled our request.
0
0
Response headers in Postman
Introduction
Checking if the server sent the correct content type like JSON or HTML.
Verifying if the server set cookies or authentication tokens.
Confirming caching rules to see if data can be stored for faster loading.
Debugging why a request failed by looking at server messages in headers.
Ensuring security headers like CORS or HSTS are present.
Syntax
Postman
pm.response.headers.get('Header-Name')Use the exact header name inside quotes to get its value.
Header names are case-insensitive but write them as they appear for clarity.
Examples
This gets the Content-Type header value and prints it.
Postman
let contentType = pm.response.headers.get('Content-Type');
console.log(contentType);This fetches the Server header to know which server software responded.
Postman
let server = pm.response.headers.get('Server');
console.log(server);This checks caching instructions from the server.
Postman
let cacheControl = pm.response.headers.get('Cache-Control');
console.log(cacheControl);Sample Program
This test checks if the response header Content-Type exactly matches 'application/json'. If yes, the test passes; otherwise, it fails.
Postman
pm.test('Check Content-Type header is application/json', () => { let contentType = pm.response.headers.get('Content-Type'); pm.expect(contentType).to.eql('application/json'); });
OutputSuccess
Important Notes
Headers can have multiple values separated by commas; get() returns the full string.
If a header is missing, get() returns null, so check for that to avoid errors.
Use Postman Console to see header values during test runs for easier debugging.
Summary
Response headers give extra info about the server's reply.
Use pm.response.headers.get('Header-Name') to read headers in Postman tests.
Check headers to verify content type, caching, security, and more.