Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
Best way to re-use javascript methods, error messages across Postman?

I have several functions that I'd like to replicate across different use cases in various requests and folders within the same collection (I'm using it as a template mostly, so it'll be pulling in variables externally)

There are many different suggestions in the Postman documentation but what's the best way to re-use code for such a use case?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

What I've been doing lately is adding functions to my collection level pre-request script like so

collectionUtils = {
    clearEnvData: function (pm) {
        some useful code
    },
    // called after every request to ensure server coverage durring smoke testing.
    cycleCurrentServer: function (serverCount, pm) {
        some useful code
    }
}

Then wherever I want to use these methods I do something like this

collectionUtils.cycleCurrentServer(index, pm);
about 4 years ago · Juan Pablo Isaza Report

0

I think generally the best way is to externalize the code into a library. You'll then make changes to the library and those changes will be reflected everywhere. Now there are several ways to implement this method, I'll leave with with the two that make sense for your use case:

Load your library from a remote site

If you are using some sort of development workflow that publishes your changes upstream, and you have your library published somewhere, you can load it at runtime:

pm.sendRequest("https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.0/dayjs.min.js", (err, res) => {
   //convert the response to text and save it as an environment variable
   pm.collectionVariables.set("dayjs_library", res.text());
 
   // eval will evaluate the JavaScript code and initialize the min.js
   eval(pm.collectionVariables.get("dayjs_library"));
 
   // you can call methods in the cdn file using this keyword
   let today = new Date();
   console.log("today=", today);
   console.log(this.dayjs(today).format())
})

Store your code on a collection variable and load it that way

A less pretty way to do it but more convenient to a lot of folks is just to drop the whole library into a collection variable like this:

enter image description here

Then you can load it when you need it:

​​const dayjs_code = pm.collectionVariables.get('dayjs_code');
 
// Invoke an anonymous function to get access to the dayjs library methods
(new Function(dayjs_code))();
 
let today = new Date();
console.log(dayjs(today).format())

In both cases when you update your library you either have to republish it or copy paste again to the collection variable. However that surely beats copy pasting a piece of code to 20 odd places and figuring out what's updated, what's not and fighting bugs while at it.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!