POST /api/v1/auth/register • HTTP 422 Unprocessable Entity
HTTP 422 Unprocessable Entity (Field Validation Errors) — Request & Response Example
Demonstrates how production REST APIs return structured validation error dictionaries so frontend forms can highlight specific input fields in red.
1. HTTP Client Request (POST)
POST /api/v1/auth/register HTTP/1.1
{
"Content-Type": "application/json"
}
{
"email": "not-an-email",
"password": "123",
"age": -5
}
2. Server JSON Response (HTTP 422 Unprocessable Entity)
{
"error": "Unprocessable Entity",
"message": "Validation failed for 3 submitted fields.",
"validation_errors": [
{
"field": "email",
"issue": "Must be a valid RFC 5322 email address."
},
{
"field": "password",
"issue": "Password must be at least 8 characters with at least one number and special character."
},
{
"field": "age",
"issue": "Age cannot be a negative number."
}
]
}
Copy-Paste Code Snippets (cURL, JavaScript Fetch, Python, Node.js)
curl -X POST "https://cometflow.space/api/v1/auth/register" \
-H "Content-Type: application/json" \
-d '{"email": "not-an-email", "password": "123", "age": -5}'
fetch('https://cometflow.space/api/v1/auth/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email: 'not-an-email', password: '123' })
}).then(async res => {
if (res.status === 422) {
const data = await res.json();
console.log('Fields to fix:', data.validation_errors);
}
});
import requests
res = requests.post('https://cometflow.space/api/v1/auth/register', json={"email": "bad"})
if res.status_code == 422:
print(res.json()['validation_errors'])
How This API Call Works Step-by-Step
- 1. Client submits registration payload.
- 2. Server runs schema validator (Zod / Joi).
- 3. Validator collects all broken field constraints.
- 4. Server responds with 422 and itemized field-by-field error list.
Backend API Functions: The validation middleware parses req.body with a schema parser and throws a 422 ValidationError if any field constraint fails.
Frequently Asked Questions
Why is 422 preferred over 400 for form validation?
422 Unprocessable Entity accurately describes that the request syntax was valid JSON, but the semantic data values failed business domain constraints.