0
votes

I'm trying to access the response of my POST request in Postman via Pre-request Script.

Script given below :

var mobiles = postman.environment.get("mobiles");

if (!mobiles)
  {
    mobiles =["8824444866","8058506668"];  
  }

var currentMobile = mobiles.shift();
postman.environement.set("mobile","currentMobile");
postman.environement.set("mobiles",mobiles);

When I click on Send button, in the postman it shows an error, which is given below:

Their was an error in evaluation the Pre-request Script: Cannot read property 'get' of undefined.

Please suggest some solution

1
obviously environment is undefined here, what does a console.log(postman) shows to you?Calvin Nunes

1 Answers

2
votes

What you were trying/looking for is postman.setEnvironmentVariable(key, value) and you can use the new one which is pm.environment.set(key, value) and of course it's shorthand for the same feature.

It's pm which has the access to the environment API and not postman. Also, you have got a typo in your script. It's environment, while you've environement in your script where you're setting the variable.

Here's the fixed script:

NOTE: This will only work on the native app

var mobiles = pm.environment.get("mobiles");

if (!mobiles)
  {
    mobiles =["8824444866","8058506668"];  
  }

var currentMobile = mobiles.shift();
pm.environment.set("mobile", currentMobile);
pm.environment.set("mobiles", mobiles);

In case you want to use postman.* API then the script for that would be:

For Chrome App & Native App (Legacy Support)

var mobiles = postman.getEnvironmentVariable("mobiles");

if (!mobiles)
  {
    mobiles =["8824444866","8058506668"];  
  }

var currentMobile = mobiles.shift();
postman.setEnvironmentVariable("mobile", currentMobile);
postman.setEnvironmentVariable("mobiles", mobiles);