HTTP 422 Unprocessable Entity (WebDAV)

Overview

The HTTP 422 Unprocessable Entity status code is used to indicate that the server understands the request but cannot process it due to semantic errors. This response is typically returned when the server encounters validation errors or business logic issues in the request payload.

Purpose

The HTTP 422 response is used to inform the client that the server cannot process the request due to issues with the request content. It is often used in the context of web APIs to convey validation failures or data format errors.

Usage

Client Behavior:

  1. Send Request: The client sends an HTTP request to the server with content that cannot be processed.
  2. Receive Response: The client receives a 422 Unprocessable Entity status code, indicating issues with the request payload.

Server Behavior:

  1. Process Request: The server processes the received request but encounters semantic errors.
  2. Send Response: The server responds with a 422 Unprocessable Entity status along with details about the validation or processing errors.

Scenarios

  • Validation Errors: When the submitted data fails validation checks on the server.
  • Semantic Errors: In cases where the request content is semantically incorrect or violates business logic rules.

Sequence Diagram

Illustrating the process for an HTTP 422 response:

sequenceDiagram
    participant Client
    participant Server as Web Server

    Note over Client: Client sends a request with invalid content
    Client->>Server: POST /invalid-resource HTTP/1.1
    Note over Server: Server identifies semantic errors
    Server->>Client: HTTP/1.1 422 Unprocessable Entity
    Server->>Client: Error Details (e.g., validation errors)

Curl Request and Response Example

Sending a request with invalid content using Curl:

curl -i -X POST -d "invalid_data" http://example.com/invalid-resource
# Expected response: HTTP/1.1 422 Unprocessable Entity
# Error details: ...

PHP cURL Request and Response Example

PHP script using cURL to handle a 422 Unprocessable Entity response:

<?php
$ch = curl_init('http://example.com/invalid-resource');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'invalid_data');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 422) {
    echo "Unprocessable Entity. Error details: " . $response;
}
curl_close($ch);
?>

Python Request and Response Example

Python script to send a request with invalid content and handle a 422 Unprocessable Entity response:

import requests
response = requests.post('http://example.com/invalid-resource', data='invalid_data')
if response.status_code == 422:
    print("Unprocessable Entity. Error details:", response.text)

Apache Configuration for HTTP 422 Unprocessable Entity

Configuring Apache to handle semantic errors and return a 422 Unprocessable Entity response:

<VirtualHost *:80>
    ServerName example.com
    <Location "/invalid-resource">
        # Configure validation and processing logic
        # Return 422 Unprocessable Entity on errors
    </Location>
</VirtualHost>

NGINX Configuration for HTTP 422 Unprocessable Entity

Setting up NGINX to handle semantic errors and respond with a 422 Unprocessable Entity status:

server {
    listen 80;
    server_name example.com;
    location /invalid-resource {
        # Configure validation and processing logic
        # Return 422 Unprocessable Entity on errors
    }
}

HTTP 420 Enhance Your Calm (Twitter) HTTP 423 Locked (WebDAV)


 

Free Weekly

Newsletter

Join my weekly newsletter for the latest in tech! You'll get neat coding tricks, trend updates, career advice, SaaS reviews, crypto, bitcoin, and financial tips. All straight to your inbox, designed to keep you ahead.