422 Unprocessable Content

The server understood the format of the request body and parsed it without trouble, but could not act on what it says. In practice: validation failed.

What 422 means

Three things are true when a server sends 422. It supports the content type (so 415 would be wrong). The syntax is correct (so it is not a plain 400 parse failure). And it still cannot carry out the instructions in the body, because they make no sense for the application: an end date before the start date, a quantity of minus two, an email address without an @.

The code began in WebDAV as "422 Unprocessable Entity" and was adopted into core HTTP in 2022 under the name "Unprocessable Content". Frameworks and client libraries still use the older name in constants and error messages. The meaning has not changed.

Reading a 422 as a client

Everything useful is in the body. APIs that use 422, such as GitHub's, and frameworks that generate it, such as Rails and FastAPI, return a list of the fields that failed and why.

  • Do not retry. The same body fails the same way.
  • Map each error to its field and show it to the user next to the input, not as one generic message.
  • If the errors make no sense, compare the JSON you send with the documented schema: a wrong nesting level, a number sent as a string, or camelCase where the API expects snake_case. The JSON formatter makes the structure easier to see.
  • With FastAPI in particular, a 422 on every request usually means the body is not arriving as JSON at all: a missing Content-Type: application/json header, or form data where a JSON model is declared.

Returning 422 well

  • Report every failure at once. One error per round trip turns a form with four mistakes into four submissions.
  • Point at the field in a way a program can use, such as a JSON Pointer (/items/0/qty) or a dotted path, and add a stable error code as well as a readable message.
  • Use a standard envelope if you have no reason not to. RFC 9457 "problem details" (application/problem+json) defines type, title, status and detail, and lets you add your own members, such as a list of field errors.
  • Keep state out of it. "This email is already registered" depends on what is stored, not on the request alone; that is a 409.
  • Do not echo secrets. Leave passwords and card numbers out of error details.

422 or 400?

The specification does not force a choice, and large, well-regarded APIs differ. A workable rule:

CodeUse whenExample body
400 Bad RequestThe body cannot be parsed, or the request is malformed at the HTTP level{"qty": 2,
422 Unprocessable ContentThe body parses and a value breaks a rule{"qty": -2}
409 ConflictThe values are valid but clash with existing stateA sku that already exists
415 Unsupported Media TypeThe format itself is not acceptedXML sent to a JSON endpoint

Using 400 for everything is also legitimate. Whichever you choose, apply it to the whole API, and make clients depend on the error body rather than on the difference between the two numbers.

Example 422 response

HTTP/1.1 422 Unprocessable Content
Content-Type: application/problem+json
Content-Length: 220

{"type":"https://api.example.com/problems/validation","title":"Validation failed","status":422,"errors":[{"pointer":"/qty","detail":"must be greater than 0"},{"pointer":"/email","detail":"is not a valid email address"}]}

Both failures are reported in one response, each with a pointer to the field. errors is an extension member that problem details allows; its shape is this API's own choice.

Defined in RFC 9110, section 15.5.21.

Questions

What is the difference between 400 and 422?

A 400 covers requests the server cannot or will not process because they are malformed. A 422 is narrower: the body is in a supported format and syntactically correct, but its contents are invalid for the operation. Many APIs use 400 for both.

Is 422 Unprocessable Entity the same as 422 Unprocessable Content?

Yes. Unprocessable Entity is the original WebDAV name; the current HTTP specification calls it Unprocessable Content. Only the label changed, and older frameworks still print the former.

Why does FastAPI return 422 for my request?

FastAPI validates the request against the types you declared and answers 422 when they do not match. The detail array in the response names each offending field. The commonest causes are a missing Content-Type: application/json header, a missing required field and a value of the wrong type.

Should I retry a 422 response?

No. Nothing about the server's state caused it, so an identical request will fail identically. Correct the data the error body points to and send the corrected request.