0
0
Postmantesting~20 mins

Body validation before sending in Postman - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Body Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why validate request body before sending in Postman?

What is the main reason to validate the request body before sending it in Postman?

ATo ensure the server receives data in the expected format and avoid errors
BTo automatically generate test reports without running tests
CTo speed up the network connection between client and server
DTo bypass authentication and send requests faster
Attempts:
2 left
💡 Hint

Think about what happens if the server gets wrong or incomplete data.

Predict Output
intermediate
2:00remaining
What is the output of this Postman test script?

Given this Postman test script that validates a JSON body field, what will be the test result?

Postman
pm.test('Check user ID is number', () => {
  const jsonData = pm.request.body.raw ? JSON.parse(pm.request.body.raw) : {};
  pm.expect(typeof jsonData.userId).to.eql('number');
});
ATest fails if userId is a number in the request body
BTest passes if userId is a number in the request body
CTest passes regardless of userId type
DTest throws a syntax error
Attempts:
2 left
💡 Hint

Look at the type check and what pm.expect compares.

assertion
advanced
2:00remaining
Which assertion correctly validates a non-empty string field in Postman?

Choose the assertion that correctly checks if the 'username' field in the JSON body is a non-empty string.

Postman
const jsonData = pm.request.body.raw ? JSON.parse(pm.request.body.raw) : {};
Apm.expect(jsonData.username).to.exist.and.to.be.empty;
Bpm.expect(jsonData.username).to.be.a('number').and.not.empty;
Cpm.expect(jsonData.username).to.be.a('string').and.not.empty;
Dpm.expect(jsonData.username).to.be.undefined;
Attempts:
2 left
💡 Hint

Check the data type and if the string is empty or not.

🔧 Debug
advanced
2:00remaining
Why does this Postman test script fail with SyntaxError?

Identify the cause of the SyntaxError in this Postman test script:

Postman
pm.test('Validate JSON body', () => {
  const data = JSON.parse(pm.request.body.raw);
  pm.expect(data.email).to.match(/^[^@\s]+@[^@\s]+\.[^@\s]+$/);
});
AThe regex pattern is missing proper escaping for backslashes
BThe test function is missing a closing brace
Cpm.expect is used incorrectly without parentheses
Dpm.request.body.raw is undefined causing JSON.parse to fail
Attempts:
2 left
💡 Hint

Check if pm.request.body.raw always contains a string before parsing.

framework
expert
3:00remaining
How to implement conditional body validation in Postman pre-request script?

You want to validate the request body only if the HTTP method is POST. Which pre-request script correctly implements this conditional validation?

A
if (pm.request.method === 'POST') {
  const body = JSON.parse(pm.request.body.raw);
  if (!body.name) throw new Error('Name is required');
}
B
if (pm.request.method = 'POST') {
  const body = JSON.parse(pm.request.body.raw);
  if (!body.name) throw new Error('Name is required');
}
C
if (pm.request.method === 'post') {
  const body = JSON.parse(pm.request.body.raw);
  if (!body.name) throw new Error('Name is required');
}
D
if (pm.request.method !== 'POST') {
  const body = JSON.parse(pm.request.body.raw);
  if (!body.name) throw new Error('Name is required');
}
Attempts:
2 left
💡 Hint

Check the equality operator and case sensitivity of HTTP methods.