Google News
logo
Postman - Interview Questions
How can we use Custom Javascript libraries in our scripts with an example?
Postman provides a lot of built-in tools and libraries that we can use to add in our pre-request or post-request scripts or test cases. Let us take the example of using the moment.js library. It provides a lot of useful functions to format data around time. Consider that we have a POST request that needs to specify the created date to the user which expects the format “DD/MM/YYYY”.

We can use the moment library to perform this using a single line of code. In our pre-request script, we need to add the below lines of code to get the correctly formatted data and then store that in an environment variable :
var moment = require('moment');
pm.environment.set('createdDate',moment().format('DD/MM/YYYY'));
There are a lot of other useful libraries like crypto.js that are useful for converting text to encrypted values which can further be used anywhere in the request body.
Advertisement