Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postman getting random UserName inside Pre-Request Scripts

I'm wanting to generate a random user to be created using the API, every time I run the request.

I have an email environment variable which I want to be set to a random email every time the user create request is sent.

I currently have:

pm.environment.set("Email", pm.globals.get("$randomUserName") + "@myDomain.co.uk");

I'm wanting to get a random user name from $randomUserName

However, I can't seem to find out how to get a value from $randomUserName in the Pre-Request Scripts section.

like image 513
Leon W Avatar asked Aug 30 '26 19:08

Leon W


1 Answers

You can use the .replaceIn() method in the sandbox.

You could do something like this:

let userName = pm.variables.replaceIn("{{$randomUserName}}")
pm.environment.set("Email", `${userName}_${Date.now()}@myDomain.co.uk`)

More info can be found here:

https://learning.postman.com/docs/postman/scripts/postman-sandbox-api-reference/#pmvariables

like image 138
Danny Dainton Avatar answered Sep 01 '26 09:09

Danny Dainton