Skip to content

422 Unprocessable Entity WebDAV (HTTP Status Code 422)

Updated: at 09:12 AM

The HTTP status code 422 represents “Unprocessable Entity.” This status code was initially defined in the WebDAV (Web Distributed Authoring and Versioning) extension but is now widely used in many other HTTP applications.

Definition

Use Cases

Distinction from Other Status Codes

Common Applications

Example Scenario

app.post("/api/register", (req, res) => {
  const { username, email, password } = req.body;

  // Validate input
  if (!username || username.length < 3) {
    return res.status(422).json({
      error: "Unprocessable Entity",
      message: "Username must be at least 3 characters long",
    });
  }

  if (!email || !email.includes("@")) {
    return res.status(422).json({
      error: "Unprocessable Entity",
      message: "Invalid email format",
    });
  }

  if (!password || password.length < 8) {
    return res.status(422).json({
      error: "Unprocessable Entity",
      message: "Password must be at least 8 characters long",
    });
  }

  // If all validations pass, proceed with registration
  // ... registration logic here ...

  res.status(201).json({ message: "User registered successfully" });
});

Example Scenario

In this example, if the username, email, or password does not meet the requirements, the API would return a 422 status code along with specific error messages.

Client Handling

Upon receiving a 422 response, the client should:

Best Practices

Security Considerations

While it is important to provide useful error messages, care should be taken not to disclose sensitive information or system details.

Original Use in WebDAV

In WebDAV, a 422 status code might be used to indicate that the requested method cannot be performed on the resource, even if the request itself is valid.

The 422 status code is highly useful in modern web development, especially when building RESTful APIs. It allows the server to explicitly indicate that the request data is semantically invalid, rather than just format-related issues, facilitating clear communication between client and server.