2

I'm trying to send a POST request to create a user using Postman. Require dynamic user email and password for that.

I'm trying assign value in new variable return from Postman inbuilt variable $randomEmail in Pre-request Script -

var new_user_email = $randomEmail;
var new_user_password = $randomPassword;
console.log(new_user_email);
console.log(new_user_password);
pm.globals.set("new_user_email", "new_user_email");
pm.globals.set("new_user_password", "new_user_password");

But it throws exception ReferenceError: $randomEmail is not defined

while if i use directly in request body like below, it works fine

{
  "email": "{{$randomEmail}}",
  "password": "{{$randomPassword}}"
}

Any idea about syntax to use it in pre-request script and store for further use ?

Nik
  • 204
  • 3
  • 20

3 Answers3

1

in script section you can not directly call $randomEmail , you have to use command pm.globals.get('varName')

0

You can't use the {{...}} syntax within the sandbox environment.

Since variables in the request builder are accessed using string substitution, they can be used everywhere in the request builder where you can add text. This includes the URL, URL parameters, headers, authorization, request body and header presets.

https://learning.getpostman.com/docs/postman/environments-and-globals/variables/#accessing-variables-in-the-request-builder

You could use the .replaceIn() function to use string substitution for that syntax.

For example:

var new_user_email = pm.variables.replaceIn({{$randomEmail}}")

Personally, I would just use the dynamic variables directly in the POST body as you have done.

Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
0

You can set the variable in the Pre-request Script tab like this: pm.globals.set("new_user_email", "{{$randomEmail}}"); pm.globals.set("new_user_password", "{{$randomPassword}}");

Tudor
  • 1