Setting headers for entire collection/folder

Hello everyone!

So I’ve been trying to use Postman/Newman for some automated API tests and I’m running into some issues.

I’ve got a collection of around 100 requests that’s expected to grow even further. In order to authorize I need to set an Authorization header, which is easy to do for an entire collection. But this specific API also expects another header for an API key. I’ve been looking through the internet and through postman but I can’t find a way to set this header for every request, except for putting the header manually in every request, which seems like a lot of work for such a simple task. Is there a way to set a header in the pre-request scripts of a collection/folder or something else that accomplishes the same goal?

7 Likes

@mauricewijniaa I am afraid the global custom headers at a collection-level is not supported as of now. Right now we support collection level authorization.

One thing you can do is add the header in request object in pre-request which is about to be sent. Something like this

pm.request.headers:HeaderList

Check here for more information. The headerList needs to be SDK instance. Check here for more info.

Thanks for the help!
When I try adding the header in the pre-request script it seems to add the new header as expected, however in the actual request the header is nowhere to be found, I’m probably doing something wrong.
This is what I’m trying to do right now:

var Header = require('postman-collection').Header
pm.request.headers.add(new Header("apiKey: " + pm.environment.get('apiKey')))
3 Likes

@mauricewijniaa Any luck getting this to work? Because I’m facing the same problem where the header is set but is not actually being sent with the request.

@_pjoshi_126 Changing pm.request.headers or request.headers sadly does not appear to be have any affect on the actual request sent. This is a real bummer when working with APIs that have custom Authentication headers.

Here is my pre-request script:

var Header = require('postman-collection').Header;

printHeaders(pm.request.headers); // before shows 2
pm.request.headers.append(Header.create('fake-api-key-value', 'X-AMC-Vendor-Key'));
printHeaders(pm.request.headers); // after shows 3 with new addition, but doesn't send

request.headers['X-AMC-Vendor-Key'] = 'fake-api-key-value'; // shot in the dark at modifying request 

function printHeaders(headers) { // print out headers to the console
    console.log('-------pm.request.headers-------')
    headers.each(function(header) {
       console.log(header.key + ': ' + header.value);
    });
}

This results in the following output, where it shows the pm.request.headers was modified, but the request sent did not include the new header.

Would be nice for this to actually work, and to have the ability to more easily specify custom headers for what is a pretty common scenario.

@RaMin0 After being stuck as well, the laziest that I am able to get is to use the Presets option on the Headers tab for each individual request. cc [@]mauricewijniaa (cant mention more than 2 people as a new user apparently)

[image would be here, but since im a new user i can only post 1 image]

Unfortunately, only built in global variables are available so I have to set my API value there.

Better than copy and paste… just

Best,
Ken

3 Likes

Postman… please help. I would if this would work.

1 Like

Just chiming in with a “yes, please”. It’d be great to have UI screens to create scoped headers similar to variables, but also just allowing it via the existing scripting functionality would be great too…

According to this discussion this, that clearly looks like a bug, is a feature request.
You may wait … and upvote for the feature request.

1 Like

according to http://www.postmanlabs.com/postman-collection/Header.html it tried

var Header = require('postman-collection').Header
pm.request.headers.add(new Header('Origin: api.uniq.test'));
console.log('request headers', pm.request.headers);

but it tells me that the headers are empty in the console…Please postman team do something…

Has there been any movement on this that we know of? I have a Pre-request script setting a header item, I can see it in the console log when I dump pm.request.headers, but it is not being submitted to the endpoint, apparently.

Ok, so the issue is still there, but I found a workaround. It seems that you can change variables before request while you can’t change headers, so the solution is to add custom headers that use variables and change them in pre-request script. Here’s an example how it can be done:

  1. open your request,
  2. add custom header “X-Username” with value “{{MyUsernameHeader}}”,
  3. add pm.variables.set("MyUsernameHeader", "myuser"); to pre-request script,
  4. send request,
  5. enjoy.
3 Likes

I have the question. I don’t want add the same set of headers for all of the requests in collection.

Any news on this bug when setting headers via pre-request?

As of 7.0.9 - You should be able to now do this in your Pre-request Scripts.

You can use the following syntax:

pm.request.headers.add({key: 'header_name', value: 'header_value' })

pm.request.headers.upsert({key: 'header_name', value: 'header_value' })

pm.request.headers.remove('header_name')

1 Like

Postman for Mac
Version 7.1.0-canary02
OS X 18.5.0 / x64

I tried:

pm.request.headers.add({key: 'header_name', value: 'header_value' });

but it doesn’t work.

Hey @lutaiphong

Not really sure what you mean by it didn’t work , it’s a little bit vague. :grin:

Are you able to expand on that more with some details about what you tried to do and where you added that code.

A screenshot would be a big help too :grin:

Sure Danny.

Here I just try to add the header header_name with value header_value to the request.

Then just submit it.
However I looked at the generated code, there is no header_name.


And on the API, I received nothing either.

Can you check the response headers tab, in the app, rather than the generated code snippet for the request please?

You mean this, right?