Bird
Raised Fist0
Postmantesting~10 mins

Body validation before sending in Postman - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the request body is not empty before sending.

Postman
if ([1]) {
  pm.sendRequest(request, callback);
}
Drag options to blanks, or click blank then click option'
Apm.request.body.text
Bpm.request.body
Cpm.request.body.raw
Dpm.request.body.json
Attempts:
3 left
💡 Hint
Common Mistakes
Using pm.request.body.json which is undefined in this context.
Checking pm.request.body without accessing raw content.
Using pm.request.body.text which is not a valid property.
2fill in blank
medium

Complete the code to validate that the JSON body contains a 'name' field before sending.

Postman
const body = JSON.parse(pm.request.body.raw);
if (body.[1]) {
  pm.sendRequest(request, callback);
}
Drag options to blanks, or click blank then click option'
Aid
Bvalue
Cemail
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 'id' or 'email' instead of 'name'.
Not parsing the JSON body before accessing fields.
Using incorrect property names.
3fill in blank
hard

Fix the error in the code to correctly validate the body length before sending.

Postman
if (pm.request.body.raw.[1] > 0) {
  pm.sendRequest(request, callback);
}
Drag options to blanks, or click blank then click option'
Alength
Bsize
Ccount
Dlength()
Attempts:
3 left
💡 Hint
Common Mistakes
Using length() which causes a runtime error.
Using size or count which are not valid string properties.
4fill in blank
hard

Fill both blanks to parse the JSON body and check if the 'age' field is greater than 18 before sending.

Postman
const data = JSON.parse(pm.request.body.[1]);
if (data.[2] > 18) {
  pm.sendRequest(request, callback);
}
Drag options to blanks, or click blank then click option'
Araw
Btext
Cage
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using pm.request.body.text which is not a valid property.
Checking a wrong field like 'value' instead of 'age'.
5fill in blank
hard

Fill all three blanks to create a validation that sends the request only if the body has a 'status' field equal to 'active'.

Postman
const body = JSON.parse(pm.request.body.[1]);
if (body.[2] === [3]) {
  pm.sendRequest(request, callback);
}
Drag options to blanks, or click blank then click option'
Araw
Bstatus
C"active"
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using pm.request.body.text which is invalid.
Comparing to 'active' without quotes causing syntax errors.
Checking wrong field names.

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

  1. Step 1: Understand the purpose of body validation

    Validating the request body helps catch errors like missing fields or wrong data types before sending.
  2. Step 2: Identify the correct reason

    Ensuring data format and required fields are correct prevents server errors and improves test reliability.
  3. Final Answer:

    To ensure the data format and required fields are correct -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    pm.expect(pm.request.body.raw).to.include('username'); -> Option C
  4. 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

  1. Step 1: Parse the request body and check type

    The script parses the JSON body and asserts that body.age is a number.
  2. Step 2: Analyze the given body value

    Since age is the string "twenty", the assertion to be.a('number') fails.
  3. Final Answer:

    The test will fail because age is a string, not a number -> Option A
  4. 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

  1. 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.
  2. Step 2: Identify missing existence check

    Without checking if email exists, the test passes because undefined is not considered empty string.
  3. Final Answer:

    The test does not check if email exists before checking emptiness -> Option D
  4. 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

  1. 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.
  2. Step 2: Prevent sending by throwing error

    Throwing an error in pre-request script stops the request from sending, enforcing validation.
  3. 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
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard