In Postman, I'm trying to achieve the following:
- In a collection:
- Create a utility object containing reusable functions
- Store that utility object in a global variable for later use in request test scripts.
- In a request:
- Pull the utility object code out of the global variables.
- Eval the code and store the resulting utility object instance in a local variable.
- Invoke a method on the utility object instance.
None of the many implementations I've seen scattered across the web seem to work, though. I can get all the way down to step 2.2, and then things just die a horrible flaming death. The JavaScript engine beneath Postman refuses to evaluate the object stored in the globals collection.
To isolate the issue, I've stripped this down to a bare minimum script, which is placed in the pre-request scripts for my collection:
postman.setGlobalVariable("loadUtils", function utils() {
let utils = {};
utils.main = function() {
console.log("Hello, world!");
}
return utils;
} + ';utils()');
I then try to load this script as follows:
var code = globals.loadUtils;
console.log(code);
var utils = eval('(' + code + ')');
But the following error always occurs:
There was an error in evaluating the test script: SyntaxError: Unexpected token ;
I tried:
- Converting the entire function to a multi-line string and storing that result in the globals environment. The same error occurred.
- Including the enclosing parentheses directly in the function body. That didn't work, either.
- Using lambda expressions, but that caused all sorts of problems in the editor itself.
I'm certain that this is something simple, stupid, and obvious, and that I am just not seeing it.
Can someone please point out what I am doing wrong here?
P.S. This should be possible, as suggested here on StackOverflow and the Postman forums on GitHub (though it requires some scrolling through the ments to see the solution).
In Postman, I'm trying to achieve the following:
- In a collection:
- Create a utility object containing reusable functions
- Store that utility object in a global variable for later use in request test scripts.
- In a request:
- Pull the utility object code out of the global variables.
- Eval the code and store the resulting utility object instance in a local variable.
- Invoke a method on the utility object instance.
None of the many implementations I've seen scattered across the web seem to work, though. I can get all the way down to step 2.2, and then things just die a horrible flaming death. The JavaScript engine beneath Postman refuses to evaluate the object stored in the globals collection.
To isolate the issue, I've stripped this down to a bare minimum script, which is placed in the pre-request scripts for my collection:
postman.setGlobalVariable("loadUtils", function utils() {
let utils = {};
utils.main = function() {
console.log("Hello, world!");
}
return utils;
} + ';utils()');
I then try to load this script as follows:
var code = globals.loadUtils;
console.log(code);
var utils = eval('(' + code + ')');
But the following error always occurs:
There was an error in evaluating the test script: SyntaxError: Unexpected token ;
I tried:
- Converting the entire function to a multi-line string and storing that result in the globals environment. The same error occurred.
- Including the enclosing parentheses directly in the function body. That didn't work, either.
- Using lambda expressions, but that caused all sorts of problems in the editor itself.
I'm certain that this is something simple, stupid, and obvious, and that I am just not seeing it.
Can someone please point out what I am doing wrong here?
P.S. This should be possible, as suggested here on StackOverflow and the Postman forums on GitHub (though it requires some scrolling through the ments to see the solution).
Share Improve this question asked Mar 26, 2019 at 18:37 Mike HoferMike Hofer 17.1k11 gold badges79 silver badges116 bronze badges 2-
What happens if you change
;utils()
to()
? – Jonas Wilms Commented Mar 26, 2019 at 18:40 -
@JonasWilms Same response.I gutted the body so that it contains
let utils = {}; return utils;
and still got theUnexpected token ;
error. – Mike Hofer Commented Mar 26, 2019 at 18:42
2 Answers
Reset to default 3You store two statements as a string, which are seperated by a semicolon:
"function utils() { /*...*/ }; utils()"
then you wrap that string in parentheses and try to execute it:
eval("(function { /*...*/ }; utils())")
that won't work, as a ;
inside of an expression is a syntax error.
You either remove the parens, replace the semicolon with a colon or use an IIFE (which I'd favor here):
eval("(" + someFunc + ")()");
Instead of using eval
, add something like the following to your Collection's pre request script.
/* You can add collection level functions to this utils object that will be available throughout
the collection's requests' pre-request scripts and tests. */
utils = {
/** This is an example function
* @param pm pass the current pm so that this has the correct context
*/
reusableFunction: function(pm) {
console.log("do something amazing here instead of just logging, including manipulating environment or request, access the response, etc. That is why you pass in the pm. Request: " + JSON.stringify(pm.request));
}
}
This function is usable in any of the collection request's pre-request script or tests.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744258819a4565529.html
评论列表(0条)