Understanding When to Use HTTP Status Codes 400 (Bad Request) vs. 422 (Unprocessable Entity)

Understanding When to Use HTTP Status Codes 400 (Bad Request) vs. 422 (Unprocessable Entity)
Photo by Markus Spiske / Unsplash

In web development, selecting the right HTTP status code is crucial for effective communication between the client and the server. Two common status codes, 400 (Bad Request) and 422 (Unprocessable Entity), are often misunderstood and misused. This blog post will clarify the differences between these codes and when to use each one.

Why Correct Status Codes Matter

Properly using HTTP status codes ensures clear communication between a server and client. For APIs and web services, this clarity helps developers understand whether an error is due to incorrect input or issues with the server’s ability to process the request.

The confusion between 400 and 422 arises because both indicate client errors. However, the distinction lies in what type of error occurred—validation issues or request processing failures.

HTTP Status Code 400 (Bad Request)

400 (Bad Request) is a general-purpose status code that signifies that something is fundamentally wrong with the client’s request, but it doesn’t necessarily relate to input validation. The server cannot process the request due to bad syntax, invalid request structure, or other issues unrelated to the content of the data being sent.

When to Use 400:

  • Incorrect request format: For example, when the request is missing mandatory parameters.
  • Invalid request syntax: When the JSON structure or data formatting is wrong.
  • General processing issues: When the server fails to process the request due to external issues (e.g., failure to communicate with a third-party service).

Example 1: Sending a Malformed Request

{
  "status": "400",
  "error": "Bad Request: Invalid JSON format"
}

Example 2: Failed Third-Party Service Integration

if (!$sent) {
    return response()->json([
        'success' => false,
        'error' => 'WhatsApp message not sent'
    ], 400);
}

In this case, 400 is appropriate because the failure is not due to a client-side validation error, but rather a processing issue (e.g., WhatsApp message delivery failure).

HTTP Status Code 422 (Unprocessable Entity)

422 (Unprocessable Entity) indicates that while the server understands the structure of the request, the content is semantically incorrect, meaning the client provided invalid or unacceptable data. This typically applies to scenarios where input validation fails.

When to Use 422:

  • Input validation errors: For example, if a required field is missing or a field contains invalid data (e.g., invalid email format or a phone number that doesn’t match the expected pattern).
  • Semantic errors: When the request is syntactically valid but contains data that violates business logic or validation rules.

Example 1: Input Validation Failure

if ($validated->fails()) {
    return response()->json([
        'success' => false,
        'error' => 'Invalid request: Validation failed'
    ], 422);
}

Example 2: Invalid Phone Number Format

if (!$numericPhone) {
    return response()->json([
        'success' => false,
        'error' => 'Invalid phone number format'
    ], 422);
}

In both cases, 422 is appropriate because the client’s request is well-formed, but the input data (e.g., invalid phone number or failed validation) is incorrect.

400 vs. 422: Key Differences

400 (Bad Request)

  • Focuses on the structure of the request.
  • Typically used for syntactical errors or processing failures.
  • Suggests that the server could not understand or process the request due to its formatting, syntax, or external service failure.

422 (Unprocessable Entity)

  • Focuses on the content of the request.
  • Used when the input data is semantically incorrect or fails validation rules.
  • Suggests that while the request is understood, the data provided cannot be processed due to validation issues.

Practical Scenarios: When to Use 400 vs. 422

Scenario 1: Sending an Incorrectly Formatted Request

If the client sends a malformed JSON request or omits mandatory fields, a 400 Bad Request status should be returned because the server cannot parse or understand the request structure.

Scenario 2: Input Validation Fails

If the client provides an email address without the "@" symbol or sends a phone number with letters instead of digits, the server should return 422 Unprocessable Entity because the input format is invalid but the request structure is correct.

Scenario 3: Third-Party Service Failure

If the client’s request is valid but an external service (e.g., a payment gateway or messaging service) fails, returning 400 Bad Request is appropriate since the issue lies in processing the request, not with the client’s input.

Conclusion: Use the Right Status Code for Clearer Communication

Understanding when to use 400 vs. 422 is key to building robust APIs and web services. While 400 covers syntactical errors and issues related to request processing, 422 is specific to validation failures where the data provided by the client doesn’t meet expected criteria.

By using the correct HTTP status code, you help developers using your API understand the specific nature of the error, whether it's a fundamental issue with the request or a problem with the input data.

Subscribe to codingwithalex

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe