0
0
Postmantesting~10 mins

Why body format matches API expectations in Postman - Test Execution Impact

Choose your learning style9 modes available
Test Overview

This test sends a POST request with a JSON body to an API endpoint. It verifies that the API accepts the body format and returns a success status code.

Test Code - Postman
Postman
pm.test("Body format matches API expectations", function () {
    const requestBody = {
        "username": "testuser",
        "password": "securePass123"
    };

    pm.sendRequest({
        url: pm.environment.get("apiUrl") + "/login",
        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', 200);
        pm.expect(res.json()).to.have.property('token').that.is.a('string');
    });
});
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test startsPostman test runner is ready-PASS
2Prepare JSON body with username and passwordRequest body is set as JSON string-PASS
3Send POST request to API endpoint /login with JSON body and Content-Type headerRequest sent to server-PASS
4Receive response from APIResponse received with status code and JSON bodyCheck no error in requestPASS
5Assert response status code is 200Response status code is 200Status code equals 200PASS
6Assert response JSON contains a token stringResponse JSON has token propertyToken property exists and is a stringPASS
7Test ends with all assertions passedTest report shows success-PASS
Failure Scenario
Failing Condition: API returns error status or missing token due to incorrect body format
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify about the API response?
AThe API returns status code 404
BThe API returns status code 200 and a token string
CThe API returns an empty body
DThe API returns status code 500
Key Result
Always ensure the request body format and Content-Type header match the API's expectations to avoid errors and get successful responses.