0
0
Postmantesting~15 mins

Why HTTP methods define API intent in Postman - Automation Benefits in Action

Choose your learning style9 modes available
Verify API behavior for different HTTP methods
Preconditions (2)
Step 1: Send a GET request to https://api.example.com/items
Step 2: Verify the response status code is 200
Step 3: Send a POST request to https://api.example.com/items with JSON body {"name": "NewItem"}
Step 4: Verify the response status code is 201 and response body contains the new item
Step 5: Send a PUT request to https://api.example.com/items/1 with JSON body {"name": "UpdatedItem"}
Step 6: Verify the response status code is 200 and response body reflects the update
Step 7: Send a DELETE request to https://api.example.com/items/1
Step 8: Verify the response status code is 204 and the item is deleted
✅ Expected Result: Each HTTP method performs its intended action: GET retrieves items, POST creates a new item, PUT updates an existing item, DELETE removes an item, with correct status codes and responses.
Automation Requirements - Postman Tests
Assertions Needed:
Status code matches expected for each HTTP method
Response body contains expected data after POST and PUT
Item is no longer retrievable after DELETE
Best Practices:
Use environment variables for base URL
Write clear and concise test scripts in Postman
Chain requests using Postman collection runner
Validate both status codes and response bodies
Automated Solution
Postman
pm.test('GET /items returns 200', function () {
    pm.response.to.have.status(200);
});

pm.test('POST /items returns 201 and new item', function () {
    pm.response.to.have.status(201);
    const jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property('name', 'NewItem');
});

pm.test('PUT /items/1 returns 200 and updated item', function () {
    pm.response.to.have.status(200);
    const jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property('name', 'UpdatedItem');
});

pm.test('DELETE /items/1 returns 204', function () {
    pm.response.to.have.status(204);
});

This Postman test script verifies the API intent defined by HTTP methods.

Each pm.test block checks the status code matches the expected one for the HTTP method used.

For POST and PUT, it also checks the response body to confirm the item was created or updated correctly.

DELETE is verified by checking the 204 No Content status code.

Using these tests ensures the API behaves as intended for each HTTP method, reflecting their purpose.

Common Mistakes - 4 Pitfalls
Not verifying the response body after POST or PUT requests
Using the wrong HTTP method for the intended action
Hardcoding URLs instead of using environment variables
Not checking the status code in tests
Bonus Challenge

Now add data-driven testing with 3 different item names for POST requests

Show Hint