ASP.NET Core Web API Interview Question #18
What is the difference between 400 Bad Request and 422 Unprocessable Content?
Web API & REST Fundamentals, Controllers, HTTP Methods & Status Codes Junior Beginner
Quick Interview Answer
400 Bad Request indicates that the server cannot process the request because the request itself is invalid, such as malformed syntax or invalid API input. With ApiController, validation failures commonly produce an automatic 400 response.
Detailed Explanation
400 Bad Request indicates that the server cannot process the request because the request itself is invalid, such as malformed syntax or invalid API input. With ApiController, validation failures commonly produce an automatic 400 response.
422 Unprocessable Content can be used when the request is syntactically valid but its content cannot be processed according to semantic rules. Teams should choose and document a consistent error strategy rather than switching unpredictably between the two.
Code Example
if (request.Quantity <= 0)
{
return BadRequest(new
{
error = "Quantity must be greater than zero."
});
}