Ari R. Fikri
Ari R. Fikri

Reputation: 331

Postman script generate different value when the variable is accessed

I tried to generate password and confirm password using POSTMAN script, but to no avail. The pre-request script I tried to generate new password is as such:

var newPass = "Ab{{$randomInt}}{{$randomInt}}!";
pm.environment.set("cs_newPassword", newPass);

in the request body I try to get the cs_newPassword variable and send it as new password and confirmation password:

{
"mobileBankerId" : "{{id_cs}}",
"currentPassword" : "{{password_cs}}",
"newPassword" : "{{cs_newPassword}}",
"confirmPassword" : "{{cs_newPassword}}"    
}

When the new password and the confirm password is accepted in the server, it has different value, and therefore it was rejected, because the new password and the password confirmation was mismatch.

When I used the "generate code snippet" feature to see the code, the variable cs_newPassword content is shown and it has same value.

Actually I called the generate password script is only once, but why it has different value when I tried to access the variable?

Upvotes: 1

Views: 1234

Answers (1)

Danny Dainton
Danny Dainton

Reputation: 25851

I don't think that you can use the global functions in that way, they only seem to work with the Headers or the URL etc.

Postman also has a few dynamic variables which you can use in your requests. > > This is primarily an experiment right now. More functions would be added soon. > Note that dynamic variables cannot be used in the Sandbox. You can only use > > them in the {{..}} format in the request URL / headers / body.

As an alternative, you could do something like this instead. The {{$randomInt}} function in Postman is a number between 0 and 1000 but you could do that in native JavaScript.

var randomInt = Math.floor((Math.random() * 1000))
var newPass = `Ab${randomInt}${randomInt}!`;
pm.environment.set("cs_newPassword", newPass);

Upvotes: 1

Related Questions