Bird
Raised Fist0
Rest APIprogramming~20 mins

422 Unprocessable Entity in Rest API - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
422 Unprocessable Entity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What does HTTP status 422 mean?

When a server responds with status code 422, what is the best description of this response?

AThe requested resource was not found on the server.
BThe server understood the request but the data was invalid or semantically incorrect.
CThe client is not authorized to access the requested resource.
DThe server encountered an unexpected error and could not complete the request.
Attempts:
2 left
💡 Hint

Think about the difference between syntax errors and semantic errors in requests.

❓ Predict Output
intermediate
1:30remaining
What is the HTTP status code returned?

Consider this REST API response after sending invalid JSON data:

{"error": "Invalid email format"}

Which HTTP status code is most appropriate?

A400 Bad Request
B404 Not Found
C422 Unprocessable Entity
D500 Internal Server Error
Attempts:
2 left
💡 Hint

Think about whether the syntax or the content is invalid.

❓ Predict Output
advanced
2:00remaining
What is the output of this API error handler?

Given this Python Flask code snippet handling a POST request:

from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/user', methods=['POST'])
def create_user():
    data = request.get_json()
    if not data or 'email' not in data:
        return jsonify({'error': 'Missing email'}), 422
    if '@' not in data['email']:
        return jsonify({'error': 'Invalid email format'}), 422
    return jsonify({'message': 'User created'}), 201

# Assume client sends: {"email": "userexample.com"}

What is the HTTP status code returned?

A500
B201
C400
D422
Attempts:
2 left
💡 Hint

Check the email validation logic and the returned status codes.

🔧 Debug
advanced
2:00remaining
Why does this API return 422 instead of 400?

Look at this Express.js code snippet:

app.post('/submit', (req, res) => {
  const { age } = req.body;
  if (typeof age !== 'number') {
    return res.status(422).json({ error: 'Age must be a number' });
  }
  res.status(200).send('Success');
});

Why is 422 used instead of 400?

ABecause the request syntax is correct but the data type is semantically wrong.
BBecause the resource was not found.
CBecause the user is unauthorized.
DBecause the server crashed processing the request.
Attempts:
2 left
💡 Hint

Think about the difference between syntax errors and semantic errors in data.

🧠 Conceptual
expert
2:30remaining
When should a REST API return 422 instead of 400?

Which scenario best describes when a REST API should return a 422 Unprocessable Entity instead of a 400 Bad Request?

AThe JSON syntax is valid but some fields fail validation rules.
BThe JSON syntax is invalid and cannot be parsed.
CThe requested URL does not exist on the server.
DThe client is not authenticated to access the resource.
Attempts:
2 left
💡 Hint

Consider the difference between syntax errors and semantic validation errors.

Practice

(1/5)
1. What does the HTTP status code 422 Unprocessable Entity indicate in a REST API?
easy
A. The server encountered an unexpected error.
B. The server understands the request but the data is invalid or incomplete.
C. The requested resource was not found on the server.
D. The client is not authorized to access the resource.

Solution

  1. Step 1: Understand HTTP status codes

    HTTP status codes communicate the result of a client's request to the server.
  2. Step 2: Identify the meaning of 422

    422 means the server understood the request but the data sent is invalid or incomplete, so it cannot process it.
  3. Final Answer:

    The server understands the request but the data is invalid or incomplete. -> Option B
  4. Quick Check:

    422 = invalid or incomplete data [OK]
Hint: 422 means data is wrong but request format is correct [OK]
Common Mistakes:
  • Confusing 422 with 404 Not Found
  • Thinking 422 means server error
  • Assuming 422 means unauthorized access
2. Which of the following is the correct way to respond with a 422 status code in a REST API using Express.js (Node.js)?
easy
A. res.sendStatus(404);
B. res.status(200).json({ message: 'Success' });
C. res.status(500).json({ error: 'Server error' });
D. res.status(422).send('Invalid input data');

Solution

  1. Step 1: Identify correct status code usage

    To send a 422 status, use res.status(422) in Express.js.
  2. Step 2: Confirm response method

    Using .send() or .json() after setting status sends the response properly.
  3. Final Answer:

    res.status(422).send('Invalid input data'); -> Option D
  4. Quick Check:

    Express 422 response = res.status(422).send() [OK]
Hint: Use res.status(422) to set code before sending response [OK]
Common Mistakes:
  • Using wrong status code like 404 or 500
  • Forgetting to set status before sending response
  • Sending 200 OK for invalid data
3. Consider this JSON response from a REST API:
{
  "status": 422,
  "errors": {
    "email": "Invalid email format",
    "password": "Password too short"
  }
}
What does this response tell the client?
medium
A. The server could not process the request because the email and password fields are invalid.
B. The server processed the request successfully with no errors.
C. The server is down and cannot handle requests.
D. The client is not authorized to access this resource.

Solution

  1. Step 1: Analyze the status code

    Status 422 means the server understood the request but found invalid data.
  2. Step 2: Interpret the errors object

    The errors show specific problems: invalid email format and password too short.
  3. Final Answer:

    The server could not process the request because the email and password fields are invalid. -> Option A
  4. Quick Check:

    422 + errors = invalid input fields [OK]
Hint: 422 with errors means input fields have problems [OK]
Common Mistakes:
  • Thinking 422 means server failure
  • Ignoring the errors object details
  • Assuming success when status is 422
4. You wrote this code snippet in a REST API controller:
if (!user.email.includes('@')) {
  res.status(422).json({ error: 'Invalid email' });
}
// further processing
But clients report they still get 200 OK even with invalid emails. What is the likely bug?
medium
A. The client is ignoring the 422 status code.
B. The status code 422 is not supported by REST APIs.
C. The function does not return after sending the 422 response, so processing continues.
D. The email validation logic is correct, so the bug is elsewhere.

Solution

  1. Step 1: Check response flow

    After sending a response, the function must stop further processing to avoid sending another response.
  2. Step 2: Identify missing return

    If return is missing before res.status(422)..., the code continues and may send 200 OK later.
  3. Final Answer:

    The function does not return after sending the 422 response, so processing continues. -> Option C
  4. Quick Check:

    Return after res.status(422) to stop processing [OK]
Hint: Always return after sending 422 response to stop code [OK]
Common Mistakes:
  • Not returning after sending response
  • Misunderstanding 422 support in REST
  • Blaming client instead of server code
5. You are designing a REST API endpoint for user registration. Which approach best uses the 422 status code to improve client communication when input data is invalid?
hard
A. Return 422 with a JSON object listing each invalid field and a clear error message.
B. Return 422 with a generic message: 'Invalid data'.
C. Return 400 Bad Request without details about the errors.
D. Return 200 OK and ignore invalid fields silently.

Solution

  1. Step 1: Understand purpose of 422

    422 is used to tell clients exactly what data is invalid to help fix input.
  2. Step 2: Choose detailed error reporting

    Providing a JSON object with each invalid field and message improves client experience.
  3. Final Answer:

    Return 422 with a JSON object listing each invalid field and a clear error message. -> Option A
  4. Quick Check:

    422 + detailed errors = better client feedback [OK]
Hint: Use 422 with detailed errors for clear client feedback [OK]
Common Mistakes:
  • Sending vague error messages
  • Using wrong status codes like 400 or 200
  • Not informing client about specific invalid fields
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