Saving responses helps you keep a record of what your API sends back. This makes it easy to check results later or share them with your team.
Saving responses in Postman
pm.test('Save response', function () { let responseBody = pm.response.text(); pm.environment.set('savedResponse', responseBody); });
pm.response.text() gets the full response as text.
pm.environment.set() saves data in Postman environment variables for reuse.
userData.pm.test('Save JSON response', function () { let jsonData = pm.response.json(); pm.environment.set('userData', JSON.stringify(jsonData)); });
id field from the JSON response.pm.test('Save specific field', function () { let jsonData = pm.response.json(); pm.environment.set('userId', jsonData.id); });
Content-Type header value from the response.pm.test('Save response header', function () { let contentType = pm.response.headers.get('Content-Type'); pm.environment.set('contentType', contentType); });
This test saves the entire response body into an environment variable named fullResponse. It also checks that the response is not empty.
pm.test('Save full response body', function () { let responseBody = pm.response.text(); pm.environment.set('fullResponse', responseBody); pm.expect(responseBody).to.not.be.empty; });
Saved responses are stored as strings. Use JSON.parse() to convert back to objects if needed.
Environment variables are shared across requests in the same environment. Use collection or global variables if needed.
Remember to clear saved variables if they are no longer needed to avoid confusion.
Saving responses helps keep track of API outputs for later use.
You can save full responses or specific parts like fields or headers.
Use Postman scripts with pm.environment.set() to save data during tests.
Practice
Solution
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.Step 2: Identify the correct purpose
Among the options, only keeping track of API outputs matches the purpose of saving responses.Final Answer:
To keep track of API outputs for later use -> Option CQuick Check:
Saving responses = keep API outputs [OK]
- Confusing saving responses with deleting requests
- Thinking it speeds up the server
- Assuming it changes API URLs
Solution
Step 1: Recall Postman scripting commands
To save data into environment variables, Postman uses the command pm.environment.set(variableName, value).Step 2: Match the command to the options
Only pm.environment.set() correctly saves a value into an environment variable.Final Answer:
pm.environment.set() -> Option BQuick Check:
Save to environment = pm.environment.set() [OK]
- Using pm.response.save() which does not exist
- Confusing get() with set()
- Trying to save with pm.request.store() which is invalid
let jsonData = pm.response.json();
pm.environment.set('userId', jsonData.id);Solution
Step 1: Parse the response JSON
The response JSON has an "id" field with value 42 and a "name" field with "Alice".Step 2: Check the script saving the value
The script saves jsonData.id into 'userId', so it saves 42.Final Answer:
42 -> Option AQuick Check:
jsonData.id = 42 [OK]
- Saving the name instead of id
- Assuming undefined if JSON parsing fails
- Confusing string and number values
let jsonData = pm.response.json();
pm.environment.set('token', jsonData.token);
What is a likely reason the token is not saved?Solution
Step 1: Check the response JSON fields
If the response JSON does not have a 'token' field, jsonData.token will be undefined.Step 2: Understand saving undefined values
Saving undefined means the environment variable 'token' will not be set correctly.Final Answer:
The response does not contain a 'token' field -> Option AQuick Check:
Missing field = no saved value [OK]
- Thinking pm.environment.set() is invalid
- Using non-existent pm.response.save()
- Assuming variable names are reserved
{
"user": {
"id": 10,
"session": {
"sessionId": "abc123"
}
}
}
Which script correctly saves 'sessionId' into an environment variable?Solution
Step 1: Analyze the JSON structure
The 'sessionId' is inside user > session > sessionId.Step 2: Match the correct path in script
Only jsonData.user.session.sessionId correctly accesses the nested 'sessionId'.Final Answer:
let jsonData = pm.response.json(); pm.environment.set('sessionId', jsonData.user.session.sessionId); -> Option DQuick Check:
Nested access = jsonData.user.session.sessionId [OK]
- Accessing sessionId directly from jsonData
- Using wrong nested keys
- Confusing sessionId with user id
Ready to prove it?
Pass the test and this pattern moves unknown
