We set variables from a response to save important data for later use in tests or requests. This helps us reuse values without typing them again.
Setting variables from response in Postman
Start learning this pattern below
Jump into concepts and practice - no test required
pm.environment.set("variable_name", value); pm.globals.set("variable_name", value); pm.collectionVariables.set("variable_name", value);
Use pm.environment.set to save variables in the current environment.
Use pm.globals.set to save variables globally across all environments.
id from the JSON response into an environment variable called userId.let jsonData = pm.response.json(); pm.environment.set("userId", jsonData.id);
Authorization header value into a global variable authToken.let token = pm.response.headers.get("Authorization"); pm.globals.set("authToken", token);
session.id from the response into a collection variable sessionId.let jsonData = pm.response.json(); pm.collectionVariables.set("sessionId", jsonData.session.id);
This test script saves the user.id from the response into an environment variable userId. It also checks that the user ID is a number and the user name is "Alice".
pm.test("Save user ID from response", function () { let jsonData = pm.response.json(); pm.environment.set("userId", jsonData.user.id); pm.expect(jsonData.user.id).to.be.a('number'); }); pm.test("Check user name", function () { let jsonData = pm.response.json(); pm.expect(jsonData.user.name).to.eql("Alice"); });
Always check that the response contains the data before setting variables to avoid errors.
Use meaningful variable names to keep your tests clear and easy to understand.
Remember environment variables are specific to the selected environment in Postman.
Setting variables from response helps reuse data across requests and tests.
Use pm.environment.set, pm.globals.set, or pm.collectionVariables.set depending on scope.
Extract values from JSON or headers and save them for later use.
Practice
Solution
Step 1: Understand variable usage in Postman
Variables store data that can be reused across requests and tests.Step 2: Identify the role of response variables
Setting variables from response allows using dynamic data from one request in others.Final Answer:
To reuse data from one request in subsequent requests or tests -> Option CQuick Check:
Variable reuse = Reuse data [OK]
- Thinking variables change HTTP methods
- Confusing variable setting with encryption
- Assuming variables generate random data
token from a JSON response field auth.token?Solution
Step 1: Identify correct method to set environment variable
Usepm.environment.set(name, value)to set environment variables.Step 2: Extract JSON response value correctly
pm.response.json()parses JSON; access nested field with.auth.token.Final Answer:
pm.environment.set('token', pm.response.json().auth.token); -> Option BQuick Check:
Set environment variable = pm.environment.set [OK]
- Using pm.environment.get instead of set
- Accessing response fields incorrectly
- Confusing pm.variables with environment variables
userId after execution?const jsonData = pm.response.json();
pm.environment.set('userId', jsonData.data[0].id);Response body:
{"data": [{"id": 42, "name": "Alice"}, {"id": 43, "name": "Bob"}]}Solution
Step 1: Parse JSON response and access first element
jsonData.data[0].idaccesses the first object's id, which is 42.Step 2: Set environment variable userId to this value
pm.environment.set stores 42 as the value of userId.Final Answer:
42 -> Option AQuick Check:
First data id = 42 [OK]
- Choosing second element's id (43)
- Assuming undefined due to wrong access
- Expecting runtime error incorrectly
pm.globals.set('sessionId', pm.response.headers.get('Session-ID'));But the variable is not set after the request. What is the most likely reason?
Solution
Step 1: Understand header retrieval in Postman
Header names are case-insensitive, but if the header is missing or name is wrong,get()returns null.Step 2: Check why variable is not set
Ifpm.response.headers.get('Session-ID')returns null, the variable is set to null or empty, appearing unset.Final Answer:
pm.response.headers.get() returns null if header is missing or name is wrong -> Option DQuick Check:
Header get returns null if missing [OK]
- Assuming header names are case-sensitive
- Believing pm.globals.set can't set from headers
- Forgetting to check if header exists
authToken from a nested JSON response where the token may sometimes be missing. Which script correctly sets authToken to the token value if present, or to an empty string if missing?Solution
Step 1: Handle optional chaining to avoid errors if token missing
Using?.safely accesses nested properties without error if missing.Step 2: Use nullish coalescing
This ensures??to set empty string if token is undefined or nullauthTokenis never undefined, avoiding test failures.Step 3: Set collection variable with the safe token value
pm.collectionVariables.set('authToken', token);stores the value correctly.Final Answer:
const token = pm.response.json()?.auth?.token ?? ''; pm.collectionVariables.set('authToken', token); -> Option AQuick Check:
Optional chaining + nullish coalescing = safe set [OK]
- Not handling missing token causing errors
- Setting variable without fallback value
- Using || which treats empty string as false
