HTTP Status Code 422 – Unprocessable Entity
The HTTP 422 Unprocessable Entity status code indicates that the server understands the content type of the request entity (e.g., JSON, XML), and the syntax of the request is correct, but it was unable to process the contained instructions. This status code is particularly useful for APIs and web applications where the client sends data that is syntactically valid but semantically incorrect.
Example 1: Invalid Data Format
Consider a scenario where a client is sending data to an API endpoint to create a new user profile. The API expects a JSON object with a correctly formatted email address. If the client sends a JSON object with an invalid email format, the server might respond with a 422 status code:
POST /api/users HTTP/1.1
Host: example.com
Content-Type: application/json
{
"username": "johndoe",
"email": "johndoe[at]example.com", // Invalid email format
"password": "securePassword123"
}
The server’s response might look like this:
HTTP/1.1 422 Unprocessable Entity
Content-Type: application/json
{
"error": "Invalid email format",
"message": "The email address 'johndoe[at]example.com' is not valid. Please provide a valid email address."
}
Example 2: Missing Required Fields
In another scenario, a client might send a request to update a product in a database. If the API requires certain fields to be present, such as a product name and price, and these fields are missing from the request, the server might also return a 422 status code:
PUT /api/products/123 HTTP/1.1
Host: example.com
Content-Type: application/json
{
"description": "A great product!"
// Missing 'name' and 'price' fields
}
The server’s response might look like this:
HTTP/1.1 422 Unprocessable Entity
Content-Type: application/json
{
"error": "Missing required fields",
"message": "The fields 'name' and 'price' are required but were not provided in the request."
}
Example 3 Scenario
# Client sends a request example. GET /example HTTP/1.1 Host: www.example.com # Server Response HTTP/1.1 422 422 Unprocessable Entity Date: Wed, 09 Oct 2024 23:06:14 GMT Server: ExampleServer/1.0 Content-Type: application/json { "error": "Description of the error for 422" }
Example 4: Another Scenario
# Client sends another example request. POST /another-example HTTP/1.1 Host: www.example.com # Server Response HTTP/1.1 422 422 Unprocessable Entity Date: Wed, 09 Oct 2024 23:06:14 GMT Server: ExampleServer/1.0 Content-Type: application/json { "error": "Detailed message for 422" }
Summary
The 422 Unprocessable Entity status code is a valuable tool for web developers and API designers to indicate that a request was well-formed and syntactically correct, but there were semantic errors that prevented the server from processing it. It helps in providing more specific feedback to the client, guiding them to correct the data they have submitted. By using 422 responses, developers can create more robust and user-friendly applications, ensuring that clients understand what went wrong and how to fix it.