Extract user ID from API response and verify it
Preconditions (2)
✅ Expected Result: The 'id' field is extracted successfully and equals 123
Jump into concepts and practice - no test required
pm.test('Status code is 200', () => { pm.response.to.have.status(200); }); const responseJson = pm.response.json(); const userId = responseJson.id; pm.test('User ID is 123', () => { pm.expect(userId).to.eql(123); });
The first test checks that the response status code is 200, which means the request was successful.
Then, we parse the JSON response using pm.response.json() to get a JavaScript object.
We extract the id field from the response object and store it in userId.
Finally, we assert that userId equals 123 using pm.expect().to.eql(). This confirms the correct data was returned.
Now add tests to extract and verify 'username' and 'email' fields from the response
userId from a JSON response and saves it as an environment variable?{"token": "abc123", "user": {"id": 42}}authToken?
let jsonData = pm.response.json();
pm.environment.set('authToken', jsonData.token);sessionId from the response:let data = pm.response.json();
pm.environment.set('sessionId', data.session_id);sessionId is always empty. What is the likely problem?sessionId not session_id -> Option C{"data": {"users": [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]}}