0
0
Postmantesting~10 mins

Why HTTP methods define API intent in Postman - Test Execution Impact

Choose your learning style9 modes available
Test Overview

This test sends requests using different HTTP methods (GET, POST, PUT, DELETE) to an API endpoint. It verifies that the API responds correctly according to the intent of each HTTP method.

Test Code - Postman
Postman
pm.test("GET request returns 200 and data", function () {
    pm.sendRequest({
        url: pm.environment.get("apiUrl") + "/items",
        method: 'GET'
    }, function (err, res) {
        pm.expect(err).to.eql(null);
        pm.expect(res).to.have.property('code', 200);
        pm.expect(res.json()).to.be.an('array');
    });
});

pm.test("POST request creates new item", function () {
    pm.sendRequest({
        url: pm.environment.get("apiUrl") + "/items",
        method: 'POST',
        header: {'Content-Type': 'application/json'},
        body: {
            mode: 'raw',
            raw: JSON.stringify({name: "NewItem"})
        }
    }, function (err, res) {
        pm.expect(err).to.eql(null);
        pm.expect(res).to.have.property('code', 201);
        pm.expect(res.json()).to.have.property('name', 'NewItem');
    });
});

pm.test("PUT request updates existing item", function () {
    pm.sendRequest({
        url: pm.environment.get("apiUrl") + "/items/1",
        method: 'PUT',
        header: {'Content-Type': 'application/json'},
        body: {
            mode: 'raw',
            raw: JSON.stringify({name: "UpdatedItem"})
        }
    }, function (err, res) {
        pm.expect(err).to.eql(null);
        pm.expect(res).to.have.property('code', 200);
        pm.expect(res.json()).to.have.property('name', 'UpdatedItem');
    });
});

pm.test("DELETE request removes item", function () {
    pm.sendRequest({
        url: pm.environment.get("apiUrl") + "/items/1",
        method: 'DELETE'
    }, function (err, res) {
        pm.expect(err).to.eql(null);
        pm.expect(res).to.have.property('code', 204);
    });
});
Execution Trace - 4 Steps
StepActionSystem StateAssertionResult
1Send GET request to /items endpointAPI server is running and has items dataResponse code is 200 and response body is an arrayPASS
2Send POST request to /items with new item dataAPI server accepts new item creationResponse code is 201 and response body contains the new item namePASS
3Send PUT request to /items/1 to update item nameAPI server updates item with id 1Response code is 200 and response body contains updated item namePASS
4Send DELETE request to /items/1 to remove itemAPI server deletes item with id 1Response code is 204 with no contentPASS
Failure Scenario
Failing Condition: API returns unexpected status code or response body does not match expected for HTTP method
Execution Trace Quiz - 3 Questions
Test your understanding
What HTTP method is used to retrieve data without changing it?
AGET
BPOST
CPUT
DDELETE
Key Result
Using the correct HTTP method clearly communicates the API's intent, making tests easier to understand and ensuring the API behaves as expected.