0
0
Postmantesting~10 mins

Body validation before sending in Postman - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks if the request body contains all required fields and valid data before sending the API request. It verifies that the body is correctly structured and prevents sending invalid data.

Test Code - Postman Test Script
Postman
pm.test("Request body has required fields and valid data", () => {
    const body = pm.request.body.raw ? JSON.parse(pm.request.body.raw) : {};
    pm.expect(body).to.have.property('username').that.is.a('string').and.is.not.empty;
    pm.expect(body).to.have.property('email').that.is.a('string').and.match(/^\S+@\S+\.\S+$/);
    pm.expect(body).to.have.property('age').that.is.a('number').and.is.above(0);
});
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test script starts and parses the request body JSONPostman request body is available as raw JSON stringCheck if body is parsed without errorPASS
2Check if 'username' field exists, is a string, and not emptyParsed body object with fields'username' property exists and is a non-empty stringPASS
3Check if 'email' field exists, is a string, and matches email patternParsed body object with fields'email' property exists and matches email regexPASS
4Check if 'age' field exists, is a number, and greater than 0Parsed body object with fields'age' property exists and is a positive numberPASS
5All validations passed, request body is validRequest ready to be sent with valid bodyAll previous assertions passedPASS
Failure Scenario
Failing Condition: Request body is missing required fields or fields have invalid data
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test check first in the request body?
AIf the response status is 200
BIf the 'age' field is zero or negative
CIf the 'username' field exists and is a non-empty string
DIf the request headers are set
Key Result
Validating the request body before sending helps catch errors early and prevents sending bad data to the server, improving test reliability and API stability.