We check the request body before sending it to make sure it has the right data. This helps avoid errors and saves time.
Body validation before sending in Postman
pm.test("Body has required fields", function () { let jsonData = pm.request.body && pm.request.body.raw ? JSON.parse(pm.request.body.raw) : {}; pm.expect(jsonData).to.have.property('name'); pm.expect(jsonData).to.have.property('email'); });
Use pm.request.body.raw to access the raw JSON body before sending.
Use pm.expect assertions to check if required fields exist.
pm.test("Body contains 'username' and 'password'", function () { let body = JSON.parse(pm.request.body.raw); pm.expect(body).to.have.property('username'); pm.expect(body).to.have.property('password'); });
pm.test("Body 'age' is a number", function () { let body = JSON.parse(pm.request.body.raw); pm.expect(body.age).to.be.a('number'); });
pm.test("Body is not empty", function () { pm.expect(pm.request.body.raw).to.not.be.empty; });
This Postman test script checks that the request body is not empty and contains 'title' and 'description' fields as strings before sending the request.
pm.test("Validate request body before sending", function () { let bodyText = pm.request.body.raw; pm.expect(bodyText).to.not.be.empty; let body = JSON.parse(bodyText); pm.expect(body).to.have.property('title'); pm.expect(body).to.have.property('description'); pm.expect(body.title).to.be.a('string'); pm.expect(body.description).to.be.a('string'); });
Always parse the raw body safely to avoid errors if the body is empty or not JSON.
Use clear and simple assertions to catch missing or wrong data early.
Running these tests before sending helps prevent server errors and saves debugging time.
Check the request body before sending to avoid errors.
Use Postman tests with pm.request.body.raw and pm.expect to validate data.
Validate required fields and data types to ensure correct requests.
Practice
Solution
Step 1: Understand the purpose of body validation
Validating the request body helps catch errors like missing fields or wrong data types before sending.Step 2: Identify the correct reason
Ensuring data format and required fields are correct prevents server errors and improves test reliability.Final Answer:
To ensure the data format and required fields are correct -> Option BQuick Check:
Body validation = correct data format [OK]
- Thinking validation speeds up sending
- Confusing validation with response generation
- Assuming validation changes HTTP method
username before sending?Solution
Step 1: Identify correct method to check raw body content
pm.request.body.raw returns the raw string body; using pm.expect(...).to.include('username') checks if 'username' text exists.Step 2: Eliminate incorrect options
pm.request.body.raw.includes('username'); returns boolean but no assertion; pm.expect(pm.response.body).to.have.property('username'); checks response, not request; pm.request.body.json().has('username'); uses invalid method.Final Answer:
pm.expect(pm.request.body.raw).to.include('username'); -> Option CQuick Check:
Use pm.expect with raw body to check text [OK]
- Checking response instead of request body
- Using non-existent methods on pm.request.body
- Not using pm.expect for assertions
const body = JSON.parse(pm.request.body.raw);
pm.expect(body.age).to.be.a('number');
What will happen if the request body is {"age": "twenty"}?Solution
Step 1: Parse the request body and check type
The script parses the JSON body and asserts that body.age is a number.Step 2: Analyze the given body value
Since age is the string "twenty", the assertion to be.a('number') fails.Final Answer:
The test will fail because age is a string, not a number -> Option AQuick Check:
Type check fails if data type mismatches [OK]
- Assuming string 'twenty' counts as number
- Expecting syntax error on valid JSON
- Thinking test skips on failure
email field:
const body = JSON.parse(pm.request.body.raw); pm.expect(body.email).to.not.be.empty;But the test always passes even when
email is missing. What is the likely problem?Solution
Step 1: Understand the test logic
The test checks if body.email is not empty, but if email is undefined, the assertion does not fail as expected.Step 2: Identify missing existence check
Without checking if email exists, the test passes because undefined is not considered empty string.Final Answer:
The test does not check ifemailexists before checking emptiness -> Option DQuick Check:
Check existence before emptiness to avoid false passes [OK]
- Assuming empty check covers missing fields
- Believing JSON.parse fails silently
- Thinking raw body is unavailable in pre-request
id or if id is not a positive integer. Which Postman pre-request script correctly validates this before sending?Solution
Step 1: Check for existence and type with condition
const body = JSON.parse(pm.request.body.raw); if (!body.id || typeof body.id !== 'number' || body.id <= 0) { throw new Error('Invalid id'); } parses the body and uses an if statement to check if id is missing, not a number, or not positive.Step 2: Prevent sending by throwing error
Throwing an error in pre-request script stops the request from sending, enforcing validation.Final Answer:
const body = JSON.parse(pm.request.body.raw); if (!body.id || typeof body.id !== 'number' || body.id <= 0) { throw new Error('Invalid id'); } -> Option AQuick Check:
Throw error to block invalid body before sending [OK]
- Using pm.expect without blocking request
- Only logging missing fields without stopping
- Checking raw string instead of parsed JSON
Ready to prove it?
Pass the test and this pattern moves unknown
