0
0
Postmantesting~20 mins

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

Choose your learning style9 modes available
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.