Test Overview
This test sends a POST request to create a new user and verifies that the response status is 201 and the returned data matches the sent data.
This test sends a POST request to create a new user and verifies that the response status is 201 and the returned data matches the sent data.
pm.test("Create user POST request", function () { const requestBody = { name: "John Doe", job: "Software Tester" }; pm.sendRequest({ url: "https://reqres.in/api/users", method: "POST", header: { "Content-Type": "application/json" }, body: { mode: "raw", raw: JSON.stringify(requestBody) } }, function (err, res) { pm.expect(err).to.be.null; pm.expect(res).to.have.property('status', 201); const jsonData = res.json(); pm.expect(jsonData).to.have.property('name', requestBody.name); pm.expect(jsonData).to.have.property('job', requestBody.job); pm.expect(jsonData).to.have.property('id'); pm.expect(jsonData).to.have.property('createdAt'); }); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Postman test runner is ready to execute the POST request test | — | PASS |
| 2 | Sends POST request to https://reqres.in/api/users with JSON body {name: 'John Doe', job: 'Software Tester'} | Request is sent over the network to the API server | — | PASS |
| 3 | Receives response from server | Response contains status code 201 and JSON body with created user data | Check that error is null | PASS |
| 4 | Verifies response status code is 201 | Response status code is 201 Created | pm.expect(res).to.have.property('status', 201) | PASS |
| 5 | Parses response JSON and verifies 'name' and 'job' match request | Response JSON has 'name' = 'John Doe' and 'job' = 'Software Tester' | pm.expect(jsonData).to.have.property('name', 'John Doe') and pm.expect(jsonData).to.have.property('job', 'Software Tester') | PASS |
| 6 | Verifies response JSON contains 'id' and 'createdAt' fields | Response JSON includes 'id' and 'createdAt' properties | pm.expect(jsonData).to.have.property('id') and pm.expect(jsonData).to.have.property('createdAt') | PASS |
| 7 | Test ends successfully | All assertions passed, user creation verified | — | PASS |
POST request in API testing?pm.test('Status code is 201', () => {
pm.response.to.have.status(201);
});
pm.test('Response has userId', () => {
const jsonData = pm.response.json();
pm.expect(jsonData).to.have.property('userId');
});userId in the JSON?pm.test('Check success message', () => {
pm.expect(pm.response.json().message).to.eql('Success');
});TypeError: Cannot read property 'message' of undefined. What is the most likely cause?pm.response.json() returned undefined, so accessing .message fails.name and email. Which Postman test script correctly verifies the server response includes the same email you sent and a status code 201?JSON.parse(pm.request.body.raw) to access email for comparison.res.email with req.email, ensuring they match.Pass the test and this pattern moves unknown