Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

243
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda