Code to find disabled request header

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

Here’s an outline with best practices for making your inquiry.

My question:
What is the code to find the disabled request header? Seems like pm.request.headers.has(‘key’, ‘value’) returns TRUE if Header/key is disabled/unselected in Postman if it has a value field has some value.

Details (like screenshots):

How I found the problem:
While validating the request headers

I’ve already tried:

Hi @indiasif, welcome to the community! :wave:

You are correct, by default the pm.request.headers list will return all of the headers, regardless of whether they were enabled. Here is such an example:

console.log(pm.request.headers.has("DisabledHeader"));  // outputs "true"
console.log(pm.request.headers.has("EnabledHeader"));  // outputs "true"

If you just want to get the disabled headers, you could use a filter command to select just the headers which are disabled, and then check to see whether your desired header exists in this new list:

var disabledHeaders = pm.request.headers.filter(header => header.disabled);
console.log(disabledHeaders.some(header => header.key == "DisabledHeader")); // outputs "true"
console.log(disabledHeaders.some(header => header.key == "EnabledHeader")); // outputs "false"

Hopefully this helps to solve your problem! :slight_smile:

1 Like

Thanks @neilstudd. It was very useful.