Setting headers for entire collection/folder

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')

2 Likes

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?

Can you open Postman Console and check?
View > Show Postman Console or you can click the following icon:
image

Now, once you send the request you’ll see what all headers are actually going through along with your request like so:

Also,
The headers that are dynamically generated through the pre-request script will not be shown up as a part of the code that is generated.

1 Like

YAHOO!
It worked guys.

Maybe I got mixed something earlier.

Thanks a lot.
Super cool support.

1 Like

Unfortunately still does not work for me (Postman 6.7.1, linux-4.15.0-48-generic on x64 / Ubuntu 18.04). At the level of pre-request script the additional header is successfully added to the headers collection, however it is somehow lost in the final, sent request. Which would be actually coherent with what the documentation says about request object:

This object is read-only. Changing properties of this object will have no effect.

Has something changed in the implementation recently?

Hey @carivaldi,

I can see from your message that you have 6.7.1.

We’re going through the documentation and updating this to reflect the current functionality - This will eventually be shown here:

1 Like

@dannydainton thanks for the quick response!
I can see the Ubuntu repository is a a little bit behind - I’ll try again when 7.0.9 is there.

Which method of installation of you using? If you’re using snap - You might need to use the candidate channel.

sudo snap switch --channel=candidate postman
sudo snap refresh postman

There is still 7.0.7 in the candidate channel - I switched to it and since it is not really a show stopper for me, I’ll wait patiently until 7.0.9 comes :slight_smile:

Thanks for help!

Hey @carivaldi,

The snap candidate channel has been updated to 7.1.1 - If you wanted to give the headers another go. :slight_smile: