Use response headers in test scripts
Most tests look at the body. Headers often hold the more useful facts: where the new resource lives, how many requests you have left, which request id to give the backend team when something breaks.
This post covers reading headers in scripts and using them. For headers as a security checklist, see check your API's security headers.
Reading a header
const location = pm.response.headers.get('Location');
const requestId = pm.response.headers.get('x-request-id'); // same as X-Request-Idget returns undefined when the header isn't there. If the response repeats a header, get returns the first one; pm.response.headers.all() lists every header with its name and value.
Location: follow the new resource
A 201 Created usually says where the new resource is:
pm.test('created with a Location', () => {
pm.response.to.have.status(201);
pm.response.to.have.header('Location');
});
const location = pm.response.headers.get('Location');
pm.environment.set('orderUrl', new URL(location, pm.request.url.toString()).toString());new URL(location, base) handles both absolute and relative Location values. The next request can be GET {{orderUrl}}.
Link: the next page
APIs such as GitHub's put pagination in a Link header:
Link: <https://api.example.com/items?page=3>; rel="next", <https://api.example.com/items?page=9>; rel="last"const link = pm.response.headers.get('Link') ?? '';
const next = link.split(',').map((p) => p.trim()).find((p) => p.endsWith('rel="next"'));
const nextUrl = next ? next.slice(next.indexOf('<') + 1, next.indexOf('>')) : '';
pm.environment.set('nextPageUrl', nextUrl);
if (nextUrl) pm.execution.setNextRequest(pm.info.requestName); // fetch the next pageThe request's URL is {{nextPageUrl}}, set to the first page by the request before it. Cap the number of pages if the list can be long. See how to test API pagination.
Rate limits: slow down before you hit 429
const remaining = Number(pm.response.headers.get('X-RateLimit-Remaining') ?? NaN);
if (!Number.isNaN(remaining)) {
console.log(`${pm.info.requestName}: ${remaining} requests left`);
pm.test('not about to hit the rate limit', () => pm.expect(remaining).to.be.above(5));
}Header names vary: X-RateLimit-Remaining, RateLimit-Remaining, X-Rate-Limit-Remaining. Check what your API sends. If a run trips the limit, add a delay between requests rather than retrying. More in testing rate limits and 429 responses.
Request ids: make failures traceable
Log the request id on every failure, so whoever reads the CI report can find the server log:
// Collection post-response script: runs after every request
if (pm.response.code >= 400) {
console.log(`${pm.info.requestName} failed with ${pm.response.code}, request id ${pm.response.headers.get('X-Request-Id') ?? 'none'}`);
}The console output is in the run's expanded row in the app, in the HTML report, and in the CLI with --verbose.
Content-Type and charset
pm.test('JSON in UTF-8', () => {
const type = pm.response.headers.get('Content-Type') ?? '';
pm.expect(type).to.match(/^application\/json/);
pm.expect(type.toLowerCase()).to.not.include('charset=iso-8859-1');
});The no-code version, on a folder's Tests tab so every request inherits it: Header Content-Type contains json.
Dates
Date, Last-Modified and Expires are HTTP dates, which Date.parse reads:
const serverTime = Date.parse(pm.response.headers.get('Date'));
pm.test('server clock within 2 minutes of ours', () => {
pm.expect(Math.abs(serverTime - Date.now())).to.be.below(120000);
});A drifting server clock breaks token expiry and signed URLs in ways that are hard to spot otherwise.