Dynamic Variables - How do I set/get?

Hello - I am still learning Postman but have problem with Dynamic Variables.

I would like to save {{$randomExampleEmail}} to a collectionVariable and use in multiple requests but I have problem.

Write now, I added the following to “pre-request script” in the collection folder.

pm.collectionVariables.set(“username”, “{{$randomExampleEmail}”);
console.log(pm.collectionVariables.get(“username”))

When I run the request, I get the following in Postman console
Console > {{$randomExampleEmail}

I wanted {{$randomExampleEmail} to be replaced by e-mail address but not sure what I am doing wrong. Can someone help me on this?

1 Like

The dynamic variables resolve in your request itself. You need to add them either in the body or in the query parameters.

What I’ve done in the past is start my collection off with a request to the Postman Echo url (https://postman-echo.com/post) with any variables I want to generate.

I then use the tests to pull the generated variables out of the response and assign them to my collection variables.

3 Likes