Add header to every request in Postman in pre-request script

As of Postman v7.0.9, this is now possible by adding a Pre-request Script on a collection.

To do this, go to your collection, right-click it, select Edit, and go to the Pre-request Scripts tab, where you can add your snippet, i.e.:

pm.request.headers.add({
  key: 'X-HEADER-TEST',
  value: '1'
});

For those who are trying it on postman ~ 7.10.0, you can add headers programmatically in a pre-request script, into the request or into the collection (into collection will add headers to all requests inside collection).

pm.request.headers.add({ 
    // These keys appears when you set a header by hand. Just for fun they are here
    disabled: false,
    description:{
        content: "DescriptionTest",
        type: "text/plain"
    },
    // Your header, effectively
    key: 'KeyTest', 
    name: 'NameTest', 
    // If you set a variable you can access it
    // HeaderTest here has value="ValueHeaderTest"
    value: pm.collectionVariables.get("HeaderTest")
});

The code snippet generator will not show the added header:

GET /get_info.php HTTP/1.1
Host: 192.168.15.25:8001
Content-type: application/json
User-Agent: PostmanRuntime/7.19.0
Accept: */*
Host: 192.168.15.25:8001
Accept-Encoding: gzip, deflate
Connection: keep-alive

But the Postman Console will:

GET /get_info.php HTTP/1.1
Content-type: application/json
KeyTest: ValueHeaderTest
User-Agent: PostmanRuntime/7.19.0
Accept: */*
Host: 192.168.15.25:8001
Accept-Encoding: gzip, deflate
Connection: keep-alive

Tags:

Postman