# Adding a header in a pre-request script

**URL:** <https://community.postman.com/t/adding-a-header-in-a-pre-request-script/5373>\
**Category:** Help Hub\
**Created:** [May 9, 2019, 8:46pm UTC](https://community.postman.com/t/adding-a-header-in-a-pre-request-script/5373 "2019-05-09T20:46:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nmishkin](https://avatars.discourse-cdn.com/v4/letter/n/b9e5f3/32.png) [@nmishkin](https://community.postman.com/u/nmishkin)\
**Post date:** [May 9, 2019, 8:46pm UTC](https://community.postman.com/t/adding-a-header-in-a-pre-request-script/5373/1 "2019-05-09T20:46:31Z")

</div>

I’m trying to use add a header in a pre-request script following the example described in [https://github.com/postmanlabs/postman-app-support/issues/4631#issuecomment-477766260](https://github.com/postmanlabs/postman-app-support/issues/4631#issuecomment-477766260) like so:

```
var csrfTokenName = pm.globals.get('csrfTokenName');
if (csrfTokenName) {
    var csrfTokenValue = pm.globals.get('csrfTokenValue');
    console.log("Setting CSRF header: " + csrfTokenName + ", value: " + csrfTokenValue);
    pm.request.headers.add({key: csrfTokenName, value: csrfTokenValue});
    console.log(pm.request.headers);
}

```

The first console messages shows up as expected except but the second one shows an empty array. Further, AFAICT, my new header is _not_ actually being sent to the server.

I tried this both on v7.0.1 and v7.1.1 (Canary).

What am I missing?

Thanks.

Nat

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [May 9, 2019, 9:26pm UTC](https://community.postman.com/t/adding-a-header-in-a-pre-request-script/5373/2 "2019-05-09T21:26:28Z")

</div>

Hey @nmishkin

Welcome to the community 👋

There was a similar question posted a few days ago. Are you able to take a look through this and see if any of this helps you?

> [@Setting headers for entire collection/folder](https://community.postman.com/t/setting-headers-for-entire-collection-folder/708/21):
>
> 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.

---

<div class="post-metadata">

**Author:** ![nmishkin](https://avatars.discourse-cdn.com/v4/letter/n/b9e5f3/32.png) [@nmishkin](https://community.postman.com/u/nmishkin)\
**Post date:** [May 10, 2019, 1:04pm UTC](https://community.postman.com/t/adding-a-header-in-a-pre-request-script/5373/3 "2019-05-10T13:04:08Z")

</div>

Thanks @danny-dainton. I now see by looking at the Postman Console window (and by clicking open the request there, which I didn’t think to do before–duh) that the header I added is in fact present. It’s just that my code didn’t get the required format of that header quite correct. (I guess it’s a red herring that my `console.log(pm.request.headers)` line showed an empty array.)
