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

162
Visualizações
postman scripting with static guids

I'm hoping to use Postman to generate and insert test data for an API project I'm working on. The resources in the API have foreign key constraints, so I want to be able to generate static guids for the resources, and share those IDs among all the other requests so that we can maintain the references and have valid foreign keys.

At the moment I'm generating data that I want to keep accessible to all the requests in the collection in a pre-request script at the collection level:

// set the user ID for our test user
var userId = pm.variables.replaceIn("{{$guid}}"); 
pm.collectionVariables.set("userId", userId);

// then generate a product ID
var productId = pm.variables.replaceIn("{{$guid}}"); 
pm.collectionVariables.set("productId", productId);

// create a few vendors
var vendorId1 = pm.variables.replaceIn("{{$guid}}"); 
pm.collectionVariables.set("vendorId1", vendorId1);

var vendorId2 = pm.variables.replaceIn("{{$guid}}"); 
pm.collectionVariables.set("vendorId1", vendorId1);

// etc

My thought was that I'd use the userId in all subsequent POSTs so there'd be a real user to associate a new object with, then use the vendor IDs with the product inserts, etc.

However, the pre-request script runs before each request-- I think it's re-creating all these values rather than keeping them static, so my plan to maintain integrity isn't working.

Is there a better strategy? For example, can I capture the ID of the inserted object from the request response and pass that variable on to the next request in the workflow so that it can use that ID for the associated resources?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You could test if the userId is already set, and only if it isn't set your variables, something like:

if (!pm.collectionVariables.get("userId")) {
    // set the user ID for our test user
    var userId = pm.variables.replaceIn("{{$guid}}"); 
    pm.collectionVariables.set("userId", userId);

    // set your other variables...
}
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