422 Unprocessable Content (formerly Unprocessable Entity) means the request is syntactically well-formed (valid JSON, etc.) but the values it contains are semantically invalid, so the server cannot process it. It is used for validation failures such as a malformed email, a negative age, or a missing required field.
Where 400 Bad Request signals "the request itself is broken", 422 is the more specific "we parsed the request fine, but its contents failed validation or a business rule." Many REST/JSON APIs use it for form-validation failures.
POST /api/signup HTTP/1.1
Host: api.example.com
Content-Type: application/json
{"email":"not-an-email","age":-3}HTTP/1.1 422 Unprocessable Content
Content-Type: application/json
{"errors":[{"field":"email","msg":"invalid format"},{"field":"age","msg":"must be >= 0"}]}app.post('/api/signup', (req, res) => {
const errors = validate(req.body);
if (errors.length) {
return res.status(422).json({ errors });
}
// create the user...
});@app.post('/api/signup')
def signup():
data = request.get_json()
errors = validate(data)
if errors:
return jsonify(errors=errors), 422
# ...