Bird
0
0

Given this code snippet, what status code will the client receive?

medium📝 Predict Output Q5 of 15
Rest API - HTTP Status Codes
Given this code snippet, what status code will the client receive?
app.post('/submit', (req, res) => {
if (req.body.name) {
res.status(201).send('Created');
} else {
res.status(422).send('Unprocessable Entity');
}
});
A200 if name is present, 400 if missing
B201 if name is present, 422 if missing
C422 always
D201 always
Step-by-Step Solution
Solution:
  1. Step 1: Check the condition on req.body.name

    If 'name' exists, status 201 is sent indicating resource created.
  2. Step 2: Check else branch

    If 'name' is missing, status 422 is sent indicating the server cannot process the request.
  3. Final Answer:

    201 if name is present, 422 if missing -> Option B
  4. Quick Check:

    Conditional creation status = B [OK]
Quick Trick: 201 means created, 422 means invalid input [OK]
Common Mistakes:
MISTAKES
  • Confusing 422 with 400
  • Assuming 200 instead of 201
  • Ignoring else condition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes