I am new to Postman, I want to print a response header value, in tests, if it exists.
For example if there is a RequstID header key and it has a value, I need a test to Say PASS when RequestID exists and prints its value in test result after PASS
You can use this to print all headers to the Postman console:
let headers = pm.response.headers.all();
console.log(headers)
I’m not sure what exactly you would like your test to look like or check for but here’s an example of using a specific header:
let headers = pm.response.headers.all();
let filteredHeader = headers.filter(header => header.key === "Date");
pm.test(`Response has the Date header - Header Value: ${filteredHeader[0].value}`, function () {
pm.expect(filteredHeader[0].key).to.eql('Date');
pm.expect(filteredHeader[0].value).to.be.a('string');
});