无需预请求脚本重复使用Postman动态变量值可行吗?
Reuse Same Dynamic Variable Value in Postman Request Body Without Pre-Request Script
Great question! I’ve run into this exact issue before with Postman’s dynamic variables—each time you call {{$randomFirstName}}, it generates a new random value, which is why your description and first_name fields ended up with different names.
Luckily, you can achieve the inline variable assignment you’re looking for using Postman’s built-in pm.variables.set() method directly in your request body. Here’s how it works:
{ "description": "{{$pm.variables.set('firstName', $randomFirstName)}}'s Home", "first_name": "{{firstName}}", "last_name": "{{$randomLastName}}", "more_fields": "..." }
How This Works:
$pm.variables.set('firstName', $randomFirstName)generates a single random first name, stores it in a temporary request-level variable calledfirstName, and returns the generated value immediately. This means yourdescriptionfield will still show something like"John's Home"as expected.- The
first_namefield then references the{{firstName}}variable, which pulls the exact same value stored earlier—no new random generation happens here. - This temporary variable only exists for the duration of the current request, so it won’t clutter your environment/global variables or affect other requests.
This approach keeps everything self-contained in the request body, just like you wanted, without needing to add extra pre-request script logic.
内容的提问来源于stack exchange,提问作者kaan_atakan
相关产品推荐
相关产品推荐

