
External libraries can be executed in postman if CDN equivalent of these libraries are available.
You can get CDN in equivalent from websites like: https://cdnjs.com/
Now to use momentTZ in postman, copy the below code into script section and execute it once and then comment out:
This will store the CDN equivalent of momentTZ and momentJS libraries to environment variables.
pm.sendRequest("https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.33/moment-timezone-with-data-2012-2022.min.js", (mtzErr, mtzRes) => {
pm.sendRequest("https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js", (mjsErr, mjsRes) => {
//you have to initialize the momentjs to make momenttz work so we are storing both into a variable
pm.environment.set("momentjs", mjsRes.text());
pm.environment.set("momenttz", mtzRes.text());
})
})
Now you initiate the libraries in either of the two approaches:
First Approach:
Now comment out the above code you don’t need it anymore. Now you can use momenttz anywhere in your collection’s script sections as :
eval(pm.environment.get("momentjs"));
eval(pm.environment.get("momenttz"));
console.log(this.moment.utc().tz("America/Los_Angeles", "UTC").format())
in this approach, the libraries are initialized in the local scope, so if you want to use them again then you have to run the 3 lines of code again in other script sections whenever it’s used.
Second approach:
new Function triggers function in the global scope. so you just have to execute the first two lines only ones in your collection run and it will be available globally throughout:
Add in first scripts pre-request script:
(new Function(pm.environment.get("momentjs")))();
(new Function(pm.environment.get("momenttz")))();
Now you can use momentTZ library anywhere as :
console.log(moment.utc().tz("America/Los_Angeles", "UTC").format())
























