Setting headers for entire collection/folder

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.

2 Likes

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:

https://learning.getpostman.com/docs/postman/scripts/postman_sandbox_api_reference/#pmrequest

1 Like

@danny-dainton 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:

Thank you so match!!

hi guys,

Iā€™m trying to add a header for every request under a folder. The pre-request scripts seem to be my best bet, however, I canā€™t get it to work with variables. It fails with bad syntax no matter which way I code it and I canā€™t find any documentation or thread on it.

Essentially, I need to read the values set in the environment variable since the header name and value depends on which environment youā€™re sending the request from.

So something like this:

pm.request.headers.add( { key: {{headerName}}, value: {{apiKey}} } );

Thatā€™s not valid syntax though. So how can I achieve this?

thanks! :slight_smile:

Hey @matheusGuimaraes

Have you tried this:

pm.request.headers.add( { key: pm.environment.get('headerName'), value: pm.environment.get('apiKey') } );

1 Like

Thank you, Danny!

I actually had tried that and it had failed with a syntax error, but I just tried again blindly with copy/paste from your reply and it worked so I must have missed a bracket or something and assumed it was lack of support for variables.

thank you so much and sorry for the time waste :slight_smile:

Youā€™re welcome. Glad to see you got it working. Donā€™t worry, itā€™s never a waste of anyones time. :trophy:

2 Likes

Hi,

Thanks to this post I manage to set a default header to a collection.


Thatā€™s great but, while it works and the request is sent correctly, it should be considered a workaround (or a bug to be fixed) because headers set this way are:

What do you think?

Hey @ohjimijimijimi,

Welcome to the community! :trophy:

I would expect to see that header in the Postman Console under the Request Headers and not in the request list that you would see.

Also, I wouldnā€™t expect to see it in the code snippet as thatā€™s not taking anything in the Pre-request Script as that would of been actioned at runtime.

It looks like your app is slightly behind the current version as the temp headers section has been removed / changed now. :slight_smile: