When a server responds with status code 422, what is the best description of this response?
Think about the difference between syntax errors and semantic errors in requests.
422 means the server understood the request syntax but the content was invalid or could not be processed.
Consider this REST API response after sending invalid JSON data:
{"error": "Invalid email format"}Which HTTP status code is most appropriate?
Think about whether the syntax or the content is invalid.
422 is used when the request syntax is correct but the content is invalid, like an invalid email format.
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?
Check the email validation logic and the returned status codes.
The email is missing '@', so the server returns 422 Unprocessable Entity with an error message.
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?
Think about the difference between syntax errors and semantic errors in data.
422 is used when the request is syntactically valid but the content is semantically invalid, like wrong data type.
Which scenario best describes when a REST API should return a 422 Unprocessable Entity instead of a 400 Bad Request?
Consider the difference between syntax errors and semantic validation errors.
400 is for malformed syntax, 422 is for semantically invalid content despite correct syntax.