But the test always fails with error TypeError: Cannot read property 'message' of undefined. What is the most likely cause?
medium
A. The server returned status 404
B. The test syntax is incorrect
C. Response body is empty or not valid JSON
D. The Postman app is outdated
Solution
Step 1: Understand the error message
The error means pm.response.json() returned undefined, so accessing .message fails.
Step 2: Identify cause of undefined JSON
This usually happens if response body is empty or not valid JSON, so parsing fails.
Final Answer:
Response body is empty or not valid JSON -> Option C
Quick Check:
Empty or invalid JSON causes undefined error [OK]
Hint: Check if response body is valid JSON before accessing properties [OK]
Common Mistakes:
Blaming test syntax instead of response content
Ignoring server status codes affecting response body
Assuming Postman app version causes this error
5. You want to test a POST request that creates a new user with fields name and email. Which Postman test script correctly verifies the server response includes the same email you sent and a status code 201?
hard
A. pm.test('Status 201 and email match', () => {
pm.response.to.have.status(201);
const res = pm.response.json();
pm.expect(res.email).to.eql(pm.request.body.raw.email);
});
B. pm.test('Status 201 and email match', () => {
pm.response.to.have.status(201);
const res = pm.response.json();
const req = JSON.parse(pm.request.body.raw);
pm.expect(res.email).to.eql(req.email);
});
C. pm.test('Status 201 and email match', () => {
pm.response.to.have.status(200);
const res = pm.response.json();
pm.expect(res.email).to.eql('test@example.com');
});
D. pm.test('Status 201 and email match', () => {
pm.response.to.have.status(201);
pm.expect(pm.response.json().email).to.exist;
});
Solution
Step 1: Check status code assertion
The test must check for status 201, which indicates resource creation.
Step 2: Compare response email with request email
Since request body is raw JSON string, parse it with JSON.parse(pm.request.body.raw) to access email for comparison.
Step 3: Validate correct assertion
pm.test('Status 201 and email match', () => {
pm.response.to.have.status(201);
const res = pm.response.json();
const req = JSON.parse(pm.request.body.raw);
pm.expect(res.email).to.eql(req.email);
}); correctly parses request body and compares res.email with req.email, ensuring they match.
Final Answer:
pm.test('Status 201 and email match', () => {
pm.response.to.have.status(201);
const res = pm.response.json();
const req = JSON.parse(pm.request.body.raw);
pm.expect(res.email).to.eql(req.email);
}); -> Option B
Quick Check:
Parse request body JSON before comparing fields [OK]
Hint: Parse request body JSON to compare fields in tests [OK]
Common Mistakes:
Trying to access pm.request.body.raw.email directly without parsing
Checking wrong status code like 200 instead of 201
Only checking email exists without matching request value
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.