Tarás
Tarás

Reputation: 183

How can I save value of dynamic variable in Postman?

Postman allows to generate random dummy data by using pre-defined variables, on example this one would be replaced by random company name:

{{$randomCompanyName}}

Using pre-defined variables multiple times return different values per request.

The question is how to save once generated value to the variable for further usage on example in tests, something like(it doesn't work):

pm.variables.set("company", {{$randomCompanyName}});

Thanks.

Upvotes: 4

Views: 1389

Answers (1)

Danny Dainton
Danny Dainton

Reputation: 25881

You can use the .replaceIn() function with that {{...}} syntax in the sandbox.

pm.globals.set("company", pm.variables.replaceIn('{{$randomCompanyName}}'));

I've used a global variable to store the value as you would want to use it again. You could also use either the environment or collectionVariables scope to do the same thing.

Upvotes: 3

Related Questions