0
0
Postmantesting~15 mins

POST request in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify POST request creates a new user successfully
Preconditions (3)
Step 1: Open Postman and create a new POST request
Step 2: Enter the URL https://api.example.com/users in the request URL field
Step 3: Select POST method
Step 4: Go to the Body tab and select raw JSON format
Step 5: Enter the following JSON data: {"name": "John Doe", "email": "john.doe@example.com"}
Step 6: Click Send button
Step 7: Observe the response status code and body
✅ Expected Result: Response status code is 201 Created and response body contains the new user's id, name as 'John Doe', and email as 'john.doe@example.com'
Automation Requirements - Postman test scripts (JavaScript)
Assertions Needed:
Status code is 201
Response body contains 'id' field
Response body 'name' equals 'John Doe'
Response body 'email' equals 'john.doe@example.com'
Best Practices:
Use pm.response.to.have.status() for status code assertion
Parse response JSON with pm.response.json()
Use pm.expect() for assertions
Keep test scripts clear and simple
Automated Solution
Postman
pm.test('Status code is 201', () => {
    pm.response.to.have.status(201);
});

const responseJson = pm.response.json();
pm.test('Response has id field', () => {
    pm.expect(responseJson).to.have.property('id');
});
pm.test('Name is John Doe', () => {
    pm.expect(responseJson.name).to.eql('John Doe');
});
pm.test('Email is john.doe@example.com', () => {
    pm.expect(responseJson.email).to.eql('john.doe@example.com');
});

This Postman test script checks the response after sending a POST request to create a user.

First, it verifies the status code is 201, which means the user was created successfully.

Then, it parses the response body as JSON to access its fields.

It asserts that the response contains an 'id' field, confirming the user has an identifier.

Finally, it checks that the 'name' and 'email' fields in the response match the data sent in the request.

These steps ensure the POST request worked as expected.

Common Mistakes - 3 Pitfalls
Not checking the status code before parsing response
Using incorrect JSON format in request body
Hardcoding values without verifying response fields exist
Bonus Challenge

Now add data-driven testing with 3 different user data inputs to verify POST request creates users correctly

Show Hint