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?
@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')))
@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.
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…
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:
open your request,
add custom header “X-Username” with value “{{MyUsernameHeader}}”,
add pm.variables.set("MyUsernameHeader", "myuser"); to pre-request script,