Why Your API Should Return 422 Not 400

Why Your API Should Return 422 Not 400

Not all client errors are created equal. A malformed JSON payload is fundamentally different from a syntactically valid request that fails business rules. Using 422 Unprocessable Entity instead of 400 Bad Request gives clients precise, actionable feedback and keeps your API semantically correct. This guide explains the HTTP spec reasoning, real-world examples, and when to use each status code.

The HTTP Status Code Definition

CodeNameMeaning
400Bad RequestGeneric client error. Malformed syntax, invalid framing, or deceptive request routing.
422Unprocessable EntityWell-formed request but semantically invalid. Validation failed on server-side constraints.
409ConflictRequest conflicts with current resource state.
404Not FoundResource does not exist.

Why 400 Is Too Broad

400 Bad Request is a catch-all. It tells the client something went wrong, but not what. Returning 400 for every validation failure loses critical context.

Scenario400 Says422 Says
Missing required field"Bad request""Field 'email' is required"
Invalid email format"Bad request""'email' must be a valid email address"
Password too short"Bad request""'password' must be at least 8 characters"
Wrong enum value"Bad request""'status' must be one of: active, inactive"

The Semantic Difference

HTTP 422 was introduced in RFC 4918 (WebDAV) and carries a specific meaning: the server understands the content type and syntax of the request, but the instructions contain semantically erroneous instructions.

Aspect400 Bad Request422 Unprocessable Entity
SyntaxMalformed or invalidWell-formed and valid
SemanticsN/ASemantically invalid
ValidationSyntax-levelBusiness-rule level
FixabilityOften unclearSpecific field errors
Client actionRetry or debugCorrect specific fields

Related Keywords

Developers and API designers also search for:

Real-World Validation Scenarios

Example 1: User Registration

Advertisement
Ad Space

// 422 Response { "errors": [ { "field": "email", "message": "Must be a valid email" }, { "field": "age", "message": "Must be positive" } ] } ```

Example 2: Order Creation

// 422 Response { "errors": [ { "field": "productId", "message": "Must be a valid UUID" }, { "field": "quantity", "message": "Must be at least 1" } ] } ```

Major API Style Guides

OrganizationRecommendation
Google API Design GuideUse 400 for malformed requests, 422 for semantic errors
Microsoft REST GuidelinesUse 400 for protocol errors, 422 for validation
JSON API specUse 422 for validation failures
GraphQLReturns 200 with errors array, but semantic errors
Stripe APIUses 400 for param errors, 422 for card declines
Shopify APIUses 422 for validation errors

Error Response Structure

A well-formed 422 response includes machine-readable details.

FieldPurposeExample
codeMachine-readable error codeVALIDATION_ERROR
fieldThe field that failedemail
messageHuman-readable descriptionMust be a valid email
typeError severityerror

When 400 Is Correct

ScenarioUse 400
Malformed JSONSyntax error, missing quotes
Invalid Content-TypeHeader missing or wrong
Empty request bodyNo payload when one required
Invalid URL encodingQuery string parsing fails
Unsupported HTTP methodPOST to GET-only endpoint

When 422 Is Correct

ScenarioUse 422
Required field missingField exists but is null/empty
Wrong data typeNumber passed instead of string
Invalid enum valueStatus must be active/inactive
String length violationMin 3, max 50 characters
Pattern mismatchEmail, phone, zip format
Business rule violationCan't cancel shipped order

Other Relevant Status Codes

CodeWhen to Use
400Malformed request syntax
401Missing or invalid authentication
403Authenticated but not authorized
404Resource not found
409Resource state conflict
422Semantically invalid request
429Rate limit exceeded
500Unexpected server error

Conclusion

Returning 422 Unprocessable Entity instead of 400 Bad Request for validation failures makes your API more precise, self-documenting, and easier to debug. It respects the HTTP semantics, aligns with major API style guides, and gives frontend developers the context they need to build better user experiences. Reserve 400 for truly malformed requests, and use 422 for well-formed requests that fail validation.

Frequently Asked Questions

400 Bad Request is a generic error for malformed request syntax. 422 Unprocessable Entity is for well-formed requests that contain semantically invalid data, such as missing required fields or invalid formats. Think of 400 as syntax errors and 422 as validation errors.

Use 422 when the request is syntactically correct (valid JSON, correct Content-Type) but fails business validation rules: missing required fields, invalid email formats, enum mismatches, or constraint violations like passwords that are too short.

422 was defined in RFC 4918 for WebDAV, but HTTP status codes are not restricted to their original extensions. 422 has become the de facto standard for validation errors across REST APIs, used by Stripe, Shopify, Google, and many others.

Not always. Use 400 for malformed JSON, invalid Content-Type headers, or empty bodies. Reserve 422 for requests that parse correctly but violate semantic rules like required fields, type mismatches, or format constraints.

Include a structured error object with field-level details. Common patterns include: an errors array with field and message pairs, an error code, and a human-readable description. This allows clients to display precise error messages to users.

Not typically. GraphQL usually returns HTTP 200 OK even for errors, with error details in the response body. However, 422 can be used for transport-level errors like malformed queries. Check your GraphQL server's conventions.

Advertisement