How can I use other packages than moments in postman’s pre-request scripts?
Welcome to the Postman community!
Only specific packages can be used in the sandbox environment our of the box:
There are other ways to bring in different modules, a couple of ways are mentioned in this Postman Answers Public Collection:
Hi, I now have this code:
const moment_code = pm.collectionVariables.get('moment_code');
(new Function(moment_code))();
let date = new Date();
pm.globals.set("currentdate", moment(date).tz("Asia/Shanghai").format("YYMMDD"))
And set my variable properly
cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.44/moment-timezone.min.js
Why isn’t it working?
There was an error in evaluating the Pre-request Script:TypeError: Cannot read properties of undefined (reading ‘version’)
Moment Timezone | Docs (momentjs.com)
To use moment-timezone, you will need [email protected]+
, moment-timezone.js
, and the moment-timezone
data.
This isn’t something you can import into Postman using the Eval method as it has dependancies.
You can only use libraries that are completely standalone where everything is contained in the minified JS.
I assume you are trying to do this because the sandbox version of moment doesn’t include the timezone plugin.
Hi, so how can I get the current time in a timezone inside of Postman?
I literally found now way.
I don’t think you can with the moment library that is available in the Postman sandbox.
You may want to consider calling another API to return the timezone information.
For example…
https://developers.google.com/maps/documentation/timezone/overview
But now I have the problem that postman does not await my async function:
async function fetchDataAndSetDate() {
const jsonResponse = await fetch("http://api.timezonedb.com/v2.1/get-time-zone?key={{hastobeset}}&format=json&by=zone&zone=Asia/Shanghai");
const result = await jsonResponse.json();
const currentDateInShanghai = result.formatted;
const dateObject = new Date(currentDateInShanghai);
const formattedDate = dateObject.toISOString().slice(0, 10).replace("T", " ").replace(/-/g, "");
pm.globals.set("currentdateinshanghai", formattedDate);
}
fetchDataAndSetDate();
Why does this need to be wrapped in a function?
Can’t you just use the Postman sendRequest() function instead to call the timezone API. This will have its own response that you can parse and then do the formatting. (I don’t know if Fetch is supported in the Postman sandbox).
Or even better, can’t this be a normal GUI request where you can just parse the response in the tests tab and set the appropriate variable.
On a side note, does this really need to be a global variable? Wouldn’t a collection variable be more suitable. (Does every collection in your workspace need access to this date?)
There are quite a few topics on here about async functions, so I would recommend searching the forum first if you want to continue with that route.
Also, have a look at the following.
Send asynchronous requests with Postman’s PM API | Postman Blog
I don’t know if this helps but I have added a Collection to my Public Workspace that shows a method of using moment with moment timezone in the sandbox.
It was basically copied from something that @praveendvd had created.
Learn something new every day. Didn’t realise that you could do that.
I always thought the library had to be completely self contained.
Same - I stumbled across this after searching for something else. @praveendvd has some great little helpers dotted around the internet
I usually follow the same pattern if I need to bring in a different module but I’ve not seen it done like this for one that has a dependency on the other.
It’s not the perfect solution and but it will work and hopefully help with @fleetadmiraljakob current use case.
Works! Thank you soooo much!
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.