Variable syntax ({{variable}}) in Postman - Build an Automation Script
pm.test('Request URL uses variable value', function () { const requestUrl = pm.request.url.toString(); const userId = pm.environment.get('userId'); pm.expect(requestUrl).to.include(userId); pm.expect(requestUrl).to.not.include('{{userId}}'); }); pm.test('Response status is 200', function () { pm.response.to.have.status(200); });
The first test checks that the request URL contains the actual value of the environment variable 'userId' instead of the literal string '{{userId}}'. This confirms that Postman correctly replaced the variable syntax before sending the request.
The second test verifies that the response status code is 200, indicating the request was successful.
Using pm.environment.get('userId') ensures the test dynamically uses the variable value from the environment, making the test flexible and maintainable.
Assertions are wrapped in pm.test() blocks for clear reporting in Postman test results.
Now add data-driven testing with 3 different userId values to verify variable replacement and response success for each.
Practice
userId in a Postman request URL?Solution
Step 1: Understand Postman variable syntax
Postman uses double curly braces{{variable}}to insert variable values dynamically.Step 2: Apply syntax to the variable
To use the variableuserIduserId, you write{{userId}}in the request URL or body.Final Answer:
Use {{userId}} in the URL to insert the variable value. -> Option CQuick Check:
Variable syntax = {{variable}} [OK]
- Using single brackets or dollar sign instead of {{}}
- Writing variable name without any braces
- Confusing variable syntax with other languages
token in Postman?Solution
Step 1: Recall Postman variable syntax rules
Postman requires variables to be wrapped in double curly braces to be recognized.Step 2: Match the correct syntax for variable
The correct syntax istoken{{token}}, which tells Postman to replace it with the variable's value.Final Answer:
{{token}} -> Option BQuick Check:
Variable reference = {{variable}} [OK]
- Using single braces or brackets
- Using dollar sign prefix
- Forgetting braces entirely
baseUrl is set to https://api.example.com, what will be the full URL when the request uses {{baseUrl}}/users?Solution
Step 1: Understand variable substitution in Postman
Postman replaces{{baseUrl}}with its set value before sending the request.Step 2: Substitute
Since{{baseUrl}}with its valuebaseUrlishttps://api.example.com, the URL becomeshttps://api.example.com/users.Final Answer:
https://api.example.com/users -> Option DQuick Check:
Variable replaced URL = https://api.example.com/users [OK]
- Not replacing variable and leaving {{baseUrl}} as is
- Concatenating variable value incorrectly
- Missing slash between baseUrl and path
Authorization: Bearer {token} in the headers, but the token is not replaced. What is the error?Solution
Step 1: Check the variable syntax in the header
The header uses{token}which is missing one pair of curly braces.Step 2: Correct the syntax to
Postman requires double curly braces to recognize variables, so it should be{{token}}Authorization: Bearer {{token}}.Final Answer:
Variable syntax is incorrect; should be {{token}}. -> Option AQuick Check:
Correct variable syntax = {{variable}} [OK]
- Using single braces instead of double
- Assuming variables auto-replace without braces
- Not setting the variable value before use
sessionId inside a JSON body and a header in Postman. Which is the best practice to ensure it works correctly?Solution
Step 1: Understand variable scope and reuse in Postman
Environment variables are accessible throughout the request, including headers and body.Step 2: Use consistent variable syntax
This ensures Postman replaces the variable correctly wherever it appears.{{sessionId}}in both JSON body and headerFinal Answer:
Set sessionId as an environment variable and use {{sessionId}} in both places. -> Option AQuick Check:
Use environment variables + {{variable}} syntax for reuse [OK]
- Hardcoding values instead of variables
- Using inconsistent variable syntax
- Assuming variables auto-share without setting scope
Ready to prove it?
Pass the test and this pattern moves unknown
