Initialize postman variables using Pre-request script
Often times we need to set postman variables before call API and API requests may use other API responses. Postman offers a pre-request script section that can be used to initialize postman variables.
In the below request, I require to send a mfa-token as part of the header to consume API, but mfa-token is not a static one to hardcode in postman global variables. I had get this for each request to get updated mfa-token from other API. This can be achieved through pre-request script to set dynamic values.
Set mfa-token variable through pre-request script by consuming another API response -
Text out of Image -
const reqObject = {
url: 'http://internalUrl/authentication/mfa-token',
method: 'GET',
header: {'channel':'customer'}
};
pm.sendRequest(reqObject, (err, res) => {
token = res.json()
console.log(`fetched token ${token.tokenValue}`)
pm.environment.set("mfa-token", token.tokenValue);
});
Comments