Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

244
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda