Postman is an API platform for building and using APIs. Postman simplifies each step of the API lifecycle and streamlines collaboration so you can create better APIs—faster.
A pre-request script in Postman is failing to add a header dynamically based on the conditional retrieval of environment variables. The script attempts to add an x-functions-key header by checking for the presence of environment variables x-functions-key and master-x-functions-key. Even though the variables are retrieved and logged correctly, the header does not appear in the request, showing {{x-functions-key}} instead of the expected resolved value.
Pre-request Script Used
var functionsKey = pm.environment.get('x-functions-key');
var masterFunctionsKey = pm.environment.get('master-x-functions-key');
// Log the keys to confirm they are retrieved correctly
console.log("functionsKey:", functionsKey);
console.log("masterFunctionsKey:", masterFunctionsKey);
if (masterFunctionsKey) {
pm.request.headers.add({ key: 'x-functions-key', value: masterFunctionsKey });
} else if (functionsKey) {
pm.request.headers.add({ key: 'x-functions-key', value: functionsKey });
} else if (!pm.environment.name.toLowerCase().endsWith("localhost")) {
throw new Error("Missing x-functions-key in the environment");
}
Expected Behavior
The x-functions-key header should be added to the request with the resolved value from either master-x-functions-key or x-functions-key, based on the pre-request script’s conditional logic.
Actual Behavior
The request header shows {{x-functions-key}} instead of resolving to the actual value added by the pre-request script. This suggests that pm.request.headers.add is not functioning as expected in the pre-request script or that headers added this way are not applied to the outgoing request.
Steps To Reproduce
In the pre-request script, retrieve x-functions-key and master-x-functions-key from the environment.
Use a conditional statement to check which variable is defined and add it to the request headers using pm.request.headers.add.
Describe the Issue
A pre-request script in Postman is failing to add a header dynamically based on the conditional retrieval of environment variables. The script attempts to add an
x-functions-key
header by checking for the presence of environment variablesx-functions-key
andmaster-x-functions-key
. Even though the variables are retrieved and logged correctly, the header does not appear in the request, showing{{x-functions-key}}
instead of the expected resolved value.Pre-request Script Used
Expected Behavior
The
x-functions-key
header should be added to the request with the resolved value from eithermaster-x-functions-key
orx-functions-key
, based on the pre-request script’s conditional logic.Actual Behavior
The request header shows
{{x-functions-key}}
instead of resolving to the actual value added by the pre-request script. This suggests thatpm.request.headers.add
is not functioning as expected in the pre-request script or that headers added this way are not applied to the outgoing request.Steps To Reproduce
x-functions-key
andmaster-x-functions-key
from the environment.pm.request.headers.add
.Screenshots or Videos
Operating System
Windows
Postman Version
11.19.0
Postman Platform
Postman App
User Account Type
Signed In User
Additional Context?
No response