Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a global function in Postman?

I'd like to declare a function once in the pre-request script of my first postman request and then use it in every request thereafter. I've set plenty of variables on the postman object and as environment variables but I haven't found a way to do the same with functions.

In the pre-request script:

function wrapTest(param1, param2, param3) {
...
}

Then I've tried

  1. postman.prototype.wrap = wrapTest;
    
  2. postman.wrap = wrapTest;
    
  3. postman.setGlobalVariable("wrap", wrapTest);
    

In the request I'm attempting to use this function:

postman.wrap(one,two,three);

which results in "postman.wrap is not a function" in all cases.

like image 908
Austin Cary Avatar asked Oct 12 '16 19:10

Austin Cary


People also ask

Which function is used to set the Global variable in Postman?

We can add, update, get or remove global and environment variables at runtime using scripts in Postman. We use a global function named “pm. *” of Postman Sandbox environment. We can do it in both Pre-request Script and Tests.

What is local and Global variable in Postman?

Postman, which is used for various activities, supports the following variable scopes. Local is the smallest variable scope, followed by Data, Environment, Collection, and Global. Global Variables functions outside of the environment and are not affected by it.


1 Answers

The function can be saved as a string and then evaled when it's used.

var stringWrap = function wrapTest(param1, param2, param3) {
...
};

postman.setEnvironmentVariable("wrap", stringWrap);
var parsedFunc = eval("("+environment.wrap+")");
parsedFunc("1", 2, 3);
like image 186
Austin Cary Avatar answered Oct 18 '22 21:10

Austin Cary