I want to automatically add a header to every request in my whole collection using this pre-request script:
pm.request.headers.add({
\'key\': \"myvar\",
This copied from here, but it worked for me
https://gist.github.com/madebysid/b57985b0649d3407a7aa9de1bd327990
pm.sendRequest({
url: "https://mydomain/ers/config/endpoint",
method: 'GET',
header: {
'content-type': 'application/json',
'accept': 'application/json',
//'x-site-code': pm.environment.get("x-site-code")
'X-CSRF-TOKEN': 'fetch'
},
body: {
mode: 'raw'//,
raw: JSON.stringify({ email: pm.environment.get("email"), password: pm.environment.get("password") })
}
}, function (err, res) {
pm.environment.set("X-CSRF-TOKEN", "Bearer " + res.json().token);
});
In test section of login, use this script to remember token in Environment
var jsonData = JSON.parse(responseBody);
tests["Body contains result"] = responseBody.has("result");
var result = jsonData.result
tests["result contains user"] = result.user !== null
var user = result.user
tests["result contains token"] = result.token !== null
var token = result.token
var accessToken = token.accessToken
var refreshToken = token.refreshToken
postman.setEnvironmentVariable("accessToken", accessToken);
postman.setEnvironmentVariable("refreshToken", refreshToken);
in every call wihich requires token, use token like this in header section
Authorization = Bearer {{accessToken}}
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
This certainly works. Loose the inverted commas on key and value
pm.request.headers.add({
key: "myvar",
value: pm.environment.get("myvar")
});
I think may be you can try this way :
// Add or update an existing header
pm.request.headers.upsert({
'key': "myvar",
'value': pm.environment.get("myvar")
});
This was updated in Postman App (v7.0.9). For more reference you can refer to : https://github.com/postmanlabs/postman-app-support/issues/1947
Looks like pm.request.headers.add()
doesn't currently update the request being sent. It's been marked as a feature request: https://github.com/postmanlabs/postman-app-support/issues/4631
You might already know that you can create pre-set headers (from the Presets dropdown) to make setting your headers a little bit easier. And there's a couple options under Settings to include specific headers. But these suggestions don't automatically add a header to every request in the whole collection like you're asking about.
UPDATE: Postman added support for this in Postman App (v7.0.9).