Adding a header in a pre-request script

I’m trying to use add a header in a pre-request script following the example described in 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});

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?



Hey @nmishkin

Welcome to the community :wave:

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?

Thanks @dannydainton. 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.)

1 Like