What is body validation before sending in API testing?
It means checking the data you want to send in the request body to make sure it is correct and complete before actually sending it to the server.
Click to reveal answer
beginner
Why is body validation important before sending a request?
It helps catch errors early, prevents sending bad data, and saves time by avoiding failed requests and debugging later.
Click to reveal answer
intermediate
How can you validate the request body in Postman before sending?
You can use Pre-request Scripts in Postman to write JavaScript code that checks the body data and stops the request if the data is invalid.
Click to reveal answer
beginner
Example: What happens if a required field is missing in the body during validation?
The validation script can show an error message and prevent the request from sending until the missing field is added.
Click to reveal answer
intermediate
What is a simple way to stop a request in Postman if validation fails?
Use throw new Error('Validation failed') in the Pre-request Script to abort the current request before it runs.
Click to reveal answer
What is the main purpose of body validation before sending a request?
ATo check if the request body data is correct and complete
BTo speed up the server response
CTo change the server URL
DTo encrypt the request body
✗ Incorrect
Body validation ensures the data you send is correct and complete before sending the request.
Which Postman feature allows you to validate the request body before sending?
AEnvironment Variables
BPre-request Script
CTests tab
DCollection Runner
✗ Incorrect
Pre-request Scripts run before the request and can validate the body data.
What happens if the validation script detects missing required data in the body?
AThe request sends anyway without errors
BThe server automatically fixes the data
CThe request can be stopped from sending
DThe request body is deleted
✗ Incorrect
Validation scripts can stop the request to prevent sending bad data.
Which command in Postman stops the current request from running?
Athrow new Error('message')
Bpm.sendRequest()
Cconsole.log()
Dpm.test()
✗ Incorrect
Throwing an Error in the Pre-request Script aborts the request before it is sent.
Body validation before sending helps to:
AChange the API endpoint
BMake the UI look better
CIncrease server storage
DPrevent sending incorrect data
✗ Incorrect
Validation prevents errors by ensuring data is correct before sending.
Explain how you would validate a JSON request body in Postman before sending it.
Think about writing JavaScript code that runs before the request.
You got /4 concepts.
Why is it better to validate the request body before sending rather than after receiving a server error?
Consider the benefits of preventing errors upfront.
You got /4 concepts.
Practice
(1/5)
1. What is the main reason to validate the request body in Postman before sending it?
easy
A. To automatically generate response data
B. To ensure the data format and required fields are correct
C. To speed up the request sending process
D. To change the HTTP method of the request
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 B
Quick Check:
Body validation = correct data format [OK]
Hint: Validate body to avoid sending bad data [OK]
Common Mistakes:
Thinking validation speeds up sending
Confusing validation with response generation
Assuming validation changes HTTP method
2. Which Postman script snippet correctly checks if the raw request body contains a JSON field named username before sending?
easy
A. pm.expect(pm.response.body).to.have.property('username');
B. pm.request.body.raw.includes('username');
C. pm.expect(pm.request.body.raw).to.include('username');
D. pm.request.body.json().has('username');
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 C
Quick Check:
Use pm.expect with raw body to check text [OK]
Hint: Use pm.expect with raw body string to check fields [OK]
Common Mistakes:
Checking response instead of request body
Using non-existent methods on pm.request.body
Not using pm.expect for assertions
3. Given this Postman pre-request script snippet:
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"}?
medium
A. The test will fail because age is a string, not a number
B. The test will pass because 'twenty' is a valid value
C. The script will throw a syntax error
D. The test will be skipped automatically
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 A
Quick Check:
Type check fails if data type mismatches [OK]
Hint: Check data types carefully in JSON body [OK]
Common Mistakes:
Assuming string 'twenty' counts as number
Expecting syntax error on valid JSON
Thinking test skips on failure
4. You wrote this Postman pre-request test to check if the request body has a non-empty 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?
medium
A. The raw body is not accessible in pre-request scripts
B. pm.expect cannot check empty strings
C. JSON.parse fails silently on missing fields
D. The test does not check if email exists before checking emptiness
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 if email exists before checking emptiness -> Option D
Quick Check:
Check existence before emptiness to avoid false passes [OK]
Hint: Check field exists before checking empty [OK]
Common Mistakes:
Assuming empty check covers missing fields
Believing JSON.parse fails silently
Thinking raw body is unavailable in pre-request
5. You want to prevent sending a request if the JSON body misses the required field id or if id is not a positive integer. Which Postman pre-request script correctly validates this before sending?
hard
A. const body = JSON.parse(pm.request.body.raw);
if (!body.id || typeof body.id !== 'number' || body.id <= 0) {
throw new Error('Invalid id');
}
B. const body = JSON.parse(pm.request.body.raw);
pm.expect(body.id).to.be.a('number').and.to.be.above(0);
C. const body = pm.request.body.raw;
pm.expect(body).to.include('id');
D. const body = JSON.parse(pm.request.body.raw);
if (body.id === undefined) {
console.log('Missing id');
}
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 A
Quick Check:
Throw error to block invalid body before sending [OK]
Hint: Throw error in pre-request to block bad body [OK]
Common Mistakes:
Using pm.expect without blocking request
Only logging missing fields without stopping
Checking raw string instead of parsed JSON
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.