Bird
Raised Fist0
Postmantesting~10 mins

Saving responses in Postman - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to save the response body as a variable in Postman.

Postman
pm.test('Save response body', function() {
    var responseBody = pm.response.[1];
    pm.environment.set('responseData', responseBody);
});
Drag options to blanks, or click blank then click option'
Atext()
Bdata()
Cbody()
Djson()
Attempts:
3 left
💡 Hint
Common Mistakes
Using pm.response.json() when the response is not JSON.
Trying to access pm.response.body() which is not a valid method.
2fill in blank
medium

Complete the code to save a JSON field from the response into an environment variable.

Postman
pm.test('Save user id', function() {
    var jsonData = pm.response.[1];
    pm.environment.set('userId', jsonData.id);
});
Drag options to blanks, or click blank then click option'
Atext()
Bdata()
Cbody()
Djson()
Attempts:
3 left
💡 Hint
Common Mistakes
Using pm.response.text() and trying to access JSON fields directly.
Forgetting to parse the response before accessing properties.
3fill in blank
hard

Fix the error in the code to correctly save the response header value.

Postman
pm.test('Save content-type header', function() {
    var contentType = pm.response.headers.get([1]);
    pm.environment.set('contentType', contentType);
});
Drag options to blanks, or click blank then click option'
A'Content-Type'
BContent-Type
C'contenttype'
DcontentType
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the header name.
Using incorrect casing for the header name.
4fill in blank
hard

Fill both blanks to save a nested JSON field from the response into a variable.

Postman
pm.test('Save nested field', function() {
    var jsonData = pm.response.[1];
    pm.environment.set('city', jsonData.address[2]);
});
Drag options to blanks, or click blank then click option'
Ajson()
Btext()
C.city
D['city']
Attempts:
3 left
💡 Hint
Common Mistakes
Using text() instead of json() to parse response.
Using incorrect syntax to access nested fields.
5fill in blank
hard

Fill all three blanks to save multiple fields from the response into environment variables.

Postman
pm.test('Save multiple fields', function() {
    var data = pm.response.[1];
    pm.environment.set('name', data[2]);
    pm.environment.set('age', data[3]);
});
Drag options to blanks, or click blank then click option'
Ajson()
B.name
C.age
Dtext()
Attempts:
3 left
💡 Hint
Common Mistakes
Using text() instead of json() to parse response.
Trying to access fields without parsing JSON.

Practice

(1/5)
1. What is the main purpose of saving responses in Postman?
easy
A. To speed up the API server
B. To delete old API requests automatically
C. To keep track of API outputs for later use
D. To change the API endpoint URL

Solution

  1. Step 1: Understand the role of saving responses

    Saving responses allows testers to keep the output data from API calls for future reference or validation.
  2. Step 2: Identify the correct purpose

    Among the options, only keeping track of API outputs matches the purpose of saving responses.
  3. Final Answer:

    To keep track of API outputs for later use -> Option C
  4. Quick Check:

    Saving responses = keep API outputs [OK]
Hint: Saving responses means storing API outputs for reuse [OK]
Common Mistakes:
  • Confusing saving responses with deleting requests
  • Thinking it speeds up the server
  • Assuming it changes API URLs
2. Which Postman script command is used to save a value from a response into an environment variable?
easy
A. pm.response.save()
B. pm.environment.set()
C. pm.request.store()
D. pm.variable.get()

Solution

  1. Step 1: Recall Postman scripting commands

    To save data into environment variables, Postman uses the command pm.environment.set(variableName, value).
  2. Step 2: Match the command to the options

    Only pm.environment.set() correctly saves a value into an environment variable.
  3. Final Answer:

    pm.environment.set() -> Option B
  4. Quick Check:

    Save to environment = pm.environment.set() [OK]
Hint: Use pm.environment.set() to save response data [OK]
Common Mistakes:
  • Using pm.response.save() which does not exist
  • Confusing get() with set()
  • Trying to save with pm.request.store() which is invalid
3. Given this Postman test script, what will be the value saved in the environment variable 'userId' if the response JSON is {"id": 42, "name": "Alice"}?
let jsonData = pm.response.json();
pm.environment.set('userId', jsonData.id);
medium
A. 42
B. null
C. undefined
D. "Alice"

Solution

  1. Step 1: Parse the response JSON

    The response JSON has an "id" field with value 42 and a "name" field with "Alice".
  2. Step 2: Check the script saving the value

    The script saves jsonData.id into 'userId', so it saves 42.
  3. Final Answer:

    42 -> Option A
  4. Quick Check:

    jsonData.id = 42 [OK]
Hint: jsonData.id saves the number 42 from response [OK]
Common Mistakes:
  • Saving the name instead of id
  • Assuming undefined if JSON parsing fails
  • Confusing string and number values
4. You wrote this test script to save a token from response but it does not save correctly:
let jsonData = pm.response.json();
pm.environment.set('token', jsonData.token);
What is a likely reason the token is not saved?
medium
A. The response does not contain a 'token' field
B. pm.environment.set() is not a valid function
C. You must use pm.response.save() instead
D. The variable name 'token' is reserved

Solution

  1. Step 1: Check the response JSON fields

    If the response JSON does not have a 'token' field, jsonData.token will be undefined.
  2. Step 2: Understand saving undefined values

    Saving undefined means the environment variable 'token' will not be set correctly.
  3. Final Answer:

    The response does not contain a 'token' field -> Option A
  4. Quick Check:

    Missing field = no saved value [OK]
Hint: Check response fields before saving variables [OK]
Common Mistakes:
  • Thinking pm.environment.set() is invalid
  • Using non-existent pm.response.save()
  • Assuming variable names are reserved
5. You want to save the 'sessionId' from a nested JSON response:
{
  "user": {
    "id": 10,
    "session": {
      "sessionId": "abc123"
    }
  }
}
Which script correctly saves 'sessionId' into an environment variable?
hard
A. let jsonData = pm.response.json(); pm.environment.set('sessionId', jsonData.user.session['id']);
B. let jsonData = pm.response.json(); pm.environment.set('sessionId', jsonData.sessionId);
C. let jsonData = pm.response.json(); pm.environment.set('sessionId', jsonData.user.sessionId);
D. let jsonData = pm.response.json(); pm.environment.set('sessionId', jsonData.user.session.sessionId);

Solution

  1. Step 1: Analyze the JSON structure

    The 'sessionId' is inside user > session > sessionId.
  2. Step 2: Match the correct path in script

    Only jsonData.user.session.sessionId correctly accesses the nested 'sessionId'.
  3. Final Answer:

    let jsonData = pm.response.json(); pm.environment.set('sessionId', jsonData.user.session.sessionId); -> Option D
  4. Quick Check:

    Nested access = jsonData.user.session.sessionId [OK]
Hint: Use full path for nested JSON fields [OK]
Common Mistakes:
  • Accessing sessionId directly from jsonData
  • Using wrong nested keys
  • Confusing sessionId with user id
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard