'External library in Postman
I want to use linq.js for my assertions. Is there a way to include an external library in Postman?
Solution 1:[1]
I am doing pretty much the same than @grinderX19.
I run this once to load my global variable:
postman.setGlobalVariable("myUtils", function myUtils() {
let utils = {};
utils.function1= function function1(Arg1, Arg2){
<code>
};
utils.function2= function function2(Arg1, Arg2){
<code>
};
return utils;
} + '; myUtils();'
);
Then, I am calling it like this in Postman requests:
//import the global variable
let utils = eval(globals.myUtils);
//Call a function contained by this global variable
var var1 = utils.function1(arg1, arg2);
Hope this helps.
Solution 2:[2]
There's an open feature for that in Postman's bugtracker since 2015: Loading External JS Files #1180 but it doesn't seem they're actively working on it.
Meanwhile I'm using a workaround mentioned in one of the comments by putting a minimized custom JS in a global variable and loading it in the beginning of each script where I'm using this code:
eval(postman.getGlobalVariable("environment variable key"));
Solution 3:[3]
I found a solution at https://postman-quick-reference-guide.readthedocs.io/en/latest/libraries.html#custom-libraries
pm.sendRequest("https://example.com/your-script.js", (error, response) => {
if (error || response.code !== 200) {
pm.expect.fail('Could not load external library');
}
eval(response.text());
// YOUR CODE HERE
});
Example using jalali-moment:
pm.sendRequest("https://unpkg.com/jalali-moment/dist/jalali-moment.browser.js", (error, response) => {
if (error || response.code !== 200) {
pm.expect.fail('Could not load external library');
eval(response.text());
var currentTime=moment().locale('fa').format('YYYYMMDDHHmmss');
console.log(currentTime);
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mario |
| Solution 2 | grinderX19 |
| Solution 3 | Alireza Fattahi |
