0
0
Rest APIprogramming~20 mins

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

Choose your learning style9 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.