0
0
Postmantesting~15 mins

DELETE request in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify DELETE request removes a user
Preconditions (1)
Step 1: Open Postman
Step 2: Set method to DELETE
Step 3: Enter URL https://api.example.com/users/123
Step 4: Click Send
✅ Expected Result: Response status code is 200 or 204, and user with ID 123 no longer exists
Automation Requirements - Postman Tests
Assertions Needed:
Verify response status code is 200 or 204
Verify response body confirms deletion or is empty
Verify subsequent GET request to /users/123 returns 404
Best Practices:
Use environment variables for base URL and user ID
Add tests in Postman Tests tab using pm.* API
Chain requests to verify deletion
Automated Solution
Postman
pm.test('Status code is 200 or 204', function () {
    pm.expect(pm.response.code).to.be.oneOf([200, 204]);
});

pm.test('Response body confirms deletion or is empty', function () {
    const body = pm.response.text();
    pm.expect(body === '' || body.toLowerCase().includes('deleted')).to.be.true;
});

// Add a follow-up GET request in Postman collection runner or use pm.sendRequest
pm.sendRequest({
    url: pm.environment.get('baseUrl') + '/users/' + pm.environment.get('userId'),
    method: 'GET'
}, function (err, res) {
    pm.test('GET after DELETE returns 404', function () {
        pm.expect(res.code).to.eql(404);
    });
});

The first test checks the response status code is either 200 or 204, which are common success codes for DELETE.

The second test verifies the response body is either empty or contains a confirmation message like 'deleted'.

The pm.sendRequest function sends a GET request to the same user endpoint to confirm the user no longer exists, expecting a 404 Not Found status.

Using environment variables for base URL and user ID makes the test reusable and easy to maintain.

Common Mistakes - 3 Pitfalls
Hardcoding URLs instead of using environment variables
Not verifying the user was actually deleted with a follow-up GET request
Assuming response body always contains a confirmation message
Bonus Challenge

Now add data-driven testing with 3 different user IDs to delete

Show Hint