Postman HeaderList remote function not working

14 views Asked by At

I have this Pre-request Script:

pm.request.headers.remove("Authorization");
pm.request.removeHeader("Authorization");

But my request still comes with an Authorization header. I'm pretty sure I'm missing something stupid here. Any ideas?

1

There are 1 answers

0
Lorenzo Benevento On

As @mikee suggested I ended up making a collection wise pre-request script to handle the Authorization header. Unfortunately it looks like most of the headers set up by Postman itself are created after the Pre-request Scripts ran.

This is the script I ended up using:

let with_auth = [
    "me",
    "orders"
]
if (with_auth.includes(pm.request.url.path[0])) {
    pm.request.headers.add({
        key: "Authorization",
        value: "Bearer " + pm.variables.get("jwt")
    })
}

I use an array to keep track of which endpoints need to have the header set and a variable to store, in my case, the JWT.