Bird
Raised Fist0
Postmantesting~20 mins

Setting variables from response in Postman - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Postman Variable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Extracting a value from JSON response and setting an environment variable
Given the following Postman test script, what will be the value of the environment variable userId after running this test if the response body is {"user": {"id": 42, "name": "Alice"}}?
Postman
const responseJson = pm.response.json();
pm.environment.set("userId", responseJson.user.id);
Aundefined
B42 (number)
C"42" (string)
Dnull
Attempts:
2 left
💡 Hint
Postman environment variables store values as strings.
assertion
intermediate
2:00remaining
Validating a variable set from response header
You want to check if the Postman environment variable sessionToken matches the Authorization header value from the response. Which assertion correctly tests this?
Postman
pm.test("Session token matches Authorization header", () => {
    const authHeader = pm.response.headers.get("Authorization");
    const sessionToken = pm.environment.get("sessionToken");
    // Fill in the assertion below
});
Apm.expect(sessionToken).to.equal(authHeader);
Bpm.expect(sessionToken).to.be.true;
Cpm.expect(sessionToken).to.be(authHeader);
Dpm.expect(sessionToken).to.eql(authHeader);
Attempts:
2 left
💡 Hint
Use the correct Chai assertion method for equality.
🔧 Debug
advanced
3:00remaining
Debugging why a variable is not set from nested JSON response
You run this Postman test script but the environment variable orderId remains undefined. The response body is {"data": {"order": {"id": 1234}}}. What is the bug in the code?
Postman
const jsonData = pm.response.json();
pm.environment.set("orderId", jsonData.order.id);
AorderId variable name is reserved and cannot be set
Bpm.environment.set requires a callback function
Cpm.response.json() returns a string, not an object
DjsonData.order is undefined because the correct path is jsonData.data.order
Attempts:
2 left
💡 Hint
Check the JSON structure carefully.
framework
advanced
2:30remaining
Best practice for setting collection variables from response in Postman
Which Postman script snippet correctly sets a collection variable token from a JSON response field access_token and ensures it is available for all requests in the collection?
A
const json = pm.response.json();
pm.collectionVariables.set("token", json.access_token);
B
const json = pm.response.json();
pm.environment.set("token", json.access_token);
C
const json = pm.response.json();
pm.globals.set("token", json.access_token);
D
const json = pm.response.json();
pm.variables.set("token", json.access_token);
Attempts:
2 left
💡 Hint
Collection variables are shared across all requests in a collection.
🧠 Conceptual
expert
3:00remaining
Understanding variable scope precedence in Postman
If a variable named apiUrl exists in globals, environment, collection, and local scopes with different values, which value will Postman use when you call pm.variables.get("apiUrl") inside a request test script?
AThe value from the collection scope
BThe value from the local scope (set during the current request execution)
CThe value from the environment scope
DThe value from the global scope
Attempts:
2 left
💡 Hint
Postman uses a specific order to resolve variable values.

Practice

(1/5)
1. What is the main purpose of setting variables from a response in Postman?
easy
A. To encrypt the response data for security
B. To change the HTTP method of the request automatically
C. To reuse data from one request in subsequent requests or tests
D. To generate random data for the request body

Solution

  1. Step 1: Understand variable usage in Postman

    Variables store data that can be reused across requests and tests.
  2. Step 2: Identify the role of response variables

    Setting variables from response allows using dynamic data from one request in others.
  3. Final Answer:

    To reuse data from one request in subsequent requests or tests -> Option C
  4. Quick Check:

    Variable reuse = Reuse data [OK]
Hint: Variables store response data for reuse in later requests [OK]
Common Mistakes:
  • Thinking variables change HTTP methods
  • Confusing variable setting with encryption
  • Assuming variables generate random data
2. Which Postman script correctly sets an environment variable named token from a JSON response field auth.token?
easy
A. pm.variables.set('token', pm.response.auth.token);
B. pm.environment.set('token', pm.response.json().auth.token);
C. pm.environment.get('token', pm.response.json().auth.token);
D. pm.globals.set('token', pm.response.body.auth.token);

Solution

  1. Step 1: Identify correct method to set environment variable

    Use pm.environment.set(name, value) to set environment variables.
  2. Step 2: Extract JSON response value correctly

    pm.response.json() parses JSON; access nested field with .auth.token.
  3. Final Answer:

    pm.environment.set('token', pm.response.json().auth.token); -> Option B
  4. Quick Check:

    Set environment variable = pm.environment.set [OK]
Hint: Use pm.environment.set with pm.response.json() for JSON fields [OK]
Common Mistakes:
  • Using pm.environment.get instead of set
  • Accessing response fields incorrectly
  • Confusing pm.variables with environment variables
3. Given this Postman test script, what will be the value of the environment variable 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"}]}
medium
A. 42
B. 43
C. undefined
D. Error: jsonData.data is not iterable

Solution

  1. Step 1: Parse JSON response and access first element

    jsonData.data[0].id accesses the first object's id, which is 42.
  2. Step 2: Set environment variable userId to this value

    pm.environment.set stores 42 as the value of userId.
  3. Final Answer:

    42 -> Option A
  4. Quick Check:

    First data id = 42 [OK]
Hint: Index 0 of data array gives first id value [OK]
Common Mistakes:
  • Choosing second element's id (43)
  • Assuming undefined due to wrong access
  • Expecting runtime error incorrectly
4. You wrote this Postman test script to set a global variable from a response header:
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?
medium
A. The header name is case-sensitive and should be 'session-id'
B. You must parse the response body before setting variables
C. pm.globals.set cannot set variables from headers
D. pm.response.headers.get() returns null if header is missing or name is wrong

Solution

  1. 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.
  2. Step 2: Check why variable is not set

    If pm.response.headers.get('Session-ID') returns null, the variable is set to null or empty, appearing unset.
  3. Final Answer:

    pm.response.headers.get() returns null if header is missing or name is wrong -> Option D
  4. Quick Check:

    Header get returns null if missing [OK]
Hint: Check header name and existence before setting variable [OK]
Common Mistakes:
  • Assuming header names are case-sensitive
  • Believing pm.globals.set can't set from headers
  • Forgetting to check if header exists
5. You want to set a collection variable 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?
hard
A. const token = pm.response.json()?.auth?.token ?? ''; pm.collectionVariables.set('authToken', token);
B. pm.collectionVariables.set('authToken', pm.response.json().auth.token);
C. if(pm.response.json().auth.token) { pm.collectionVariables.set('authToken', pm.response.json().auth.token); }
D. pm.collectionVariables.set('authToken', pm.response.json().auth?.token || null);

Solution

  1. Step 1: Handle optional chaining to avoid errors if token missing

    Using ?. safely accesses nested properties without error if missing.
  2. Step 2: Use nullish coalescing ?? to set empty string if token is undefined or null

    This ensures authToken is never undefined, avoiding test failures.
  3. Step 3: Set collection variable with the safe token value

    pm.collectionVariables.set('authToken', token); stores the value correctly.
  4. Final Answer:

    const token = pm.response.json()?.auth?.token ?? ''; pm.collectionVariables.set('authToken', token); -> Option A
  5. Quick Check:

    Optional chaining + nullish coalescing = safe set [OK]
Hint: Use ?. and ?? to safely set variables from optional fields [OK]
Common Mistakes:
  • Not handling missing token causing errors
  • Setting variable without fallback value
  • Using || which treats empty string as false