What Is a 422 Status Code?
The server understands the content type of the request entity (hence a 415 Unsupported Media Type status code is inappropriate), and the syntax of the request entity is correct (thus a 400 Bad Request status code is inappropriate) but was unable to process the contained instructions.
For example, this error condition may occur if an XML request body contains well-formed (i.e., syntactically correct), but semantically erroneous, XML instructions.
- Source: RFC4918 Section 11.2
422 CODE REFERENCES
Rails HTTP Status Symbol :unprocessable_entity
Symfony HTTP Status Constant Response::HTTP_UNPROCESSABLE_ENTITY
.NET HttpStatusCode.UnprocessableEntity
Rust http::StatusCode::UNPROCESSABLE_ENTITY
Go http.StatusUnprocessableEntity
Python3.5+ http.HTTPStatus.UNPROCESSABLE_ENTITY
Apache HttpComponents Core org.apache.hc.core5.http.HttpStatus.SC_UNPROCESSABLE_ENTITY
Angular @angular/common/http/HttpStatusCode.UnprocessableEntity
422 status code example
Here’s an example request and response for a 422 Unprocessable Entity status code:
Request
POST /api/users HTTP/1.1
Host: example.com
Content-Type: application/json
{
"name": "",
"email": "example.com"
}
Response
HTTP/1.1 422 Unprocessable Entity
Content-Type: application/json
{
"error": "Unprocessable Entity",
"message": "Validation failed",
"details": [
{
"field": "name",
"message": "Name is required"
},
{
"field": "email",
"message": "Invalid email address"
}
]
}
In this example, the request is attempting to create a new user by sending a JSON payload to the server via a POST
request. However, the server is unable to process the request due to validation errors in the payload.
The server responds with a 422 Unprocessable Entity
status code and a JSON payload containing an error message, a description of the error, and details about the specific validation errors that occurred. The client can use this information to correct the errors and resubmit the request.
What causes a 422 status code?
A 422 status code is typically caused when a request is well-formed but the server is unable to process it because it contains semantic errors or does not meet certain conditions. Some common causes of a 422 status code include:
- Validation errors: The server is unable to process the request because the submitted data contains errors or is incomplete. For example, if a required field is missing or the data format is invalid.
- Business logic errors: The server is unable to fulfill the request because it violates some business rule or constraint. For example, if a user tries to register with an email address that is already in use.
- Authorization errors: The server is unable to process the request because the user is not authorized to perform the requested action. For example, if a user tries to delete a resource that they do not have permission to delete.
- Data conflicts: The server is unable to process the request because it conflicts with existing data. For example, if a user tries to update a resource that has been updated by another user in the meantime.
It is important to note that the exact cause of a 422 status code may vary depending on the specific application and its implementation.
How to fix a 422 status code
A 422 status code indicates that the server was unable to process the request because it contains invalid data. To fix a 422 status code, you need to identify the specific data that caused the issue and correct it. Here are some steps you can follow:
- Check the request payload: The 422 status code typically occurs when there is an issue with the request payload. Check the data being sent to the server and ensure that it is formatted correctly and is valid.
- Validate the input: Validate the input received by the server to ensure that it adheres to the defined structure and data types. If any of the input data is invalid, return a detailed error message to the client.
- Provide helpful error messages: Provide clear and informative error messages that can help the client identify the issue and correct it. This can help the client resolve the issue more quickly and minimize the number of 422 errors in the future.
- Test thoroughly: Ensure that your code is thoroughly tested to catch any validation errors or other issues that could cause a 422 status code.
By following these steps, you can fix the issues causing the 422 status code and ensure that the server is able to process the request successfully.
Additional resources
- Learn about web development
- Learn about SEO
- Web development services from WebFX
- SEO services from WebFX
- MDN Web Docs
- W3Schools
Marketing Tips for Niche Industries
- Tourism & Hospitality Statistics
- Travel and Tourism Marketing Budget
- Urgent Care Marketing Ideas: 5 High-ROI Tactics
- WebFX: Your Education Digital Marketing Agency
- Why Auto Part Retailers Need Digital Marketing
- Why Digital Marketing is Essential for Auctioneers
- Your Guide to Digital Marketing for Exercise Equipment Companies
- Your Guide to Digital Marketing for Industrial Repair Companies
- Vision Care Industry Statistics
- 6 Best Heavy Equipment Marketing Agencies