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, (errres=> {
    token = res.json()
    console.log(`fetched token ${token.tokenValue}`)
    pm.environment.set("mfa-token"token.tokenValue);
});

Comments

Popular posts from this blog

Windows Azure Package Build Error: The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

Resource ID : 1. The request limit for the database is 180 and has been reached.

How to get Client's Location using IPAddress