HTTP 413 Request Entity Too Large

Overview

The HTTP 413 Payload Too Large status code indicates that the server is refusing to process a request because the request payload is larger than the server is willing or able to process.

Purpose

The HTTP 413 response is used to inform the client that the data payload sent in the request is too large for the server to handle. This status code can help prevent clients from sending excessively large requests that could impact server performance.

Usage

Client Behavior:

  1. Send Large Request: The client sends an HTTP request with a payload that exceeds the server’s size limit.
  2. Receive Response: The client receives the HTTP 413 status code, indicating that the payload is too large.

Server Behavior:

  1. Evaluate Request Size: The server checks the size of the request payload.
  2. Send Response: If the payload is too large, the server responds with a 413 Payload Too Large status code.

Scenarios

  • File Uploads: When uploading files that exceed the server’s size limits.
  • Data Submission: Sending large amounts of data that surpass the server’s capacity.

Sequence Diagram

Illustrating the process for an HTTP 413 response:

sequenceDiagram
    participant Client
    participant Server as Web Server

    Note over Client: Client sends a large data payload
    Client->>Server: POST /upload HTTP/1.1
    Note over Server: Server checks payload size
    Server->>Client: HTTP/1.1 413 Payload Too Large

Curl Request and Response Example

Sending a request with a large payload using Curl:

curl -i -X POST --data-binary @largefile.txt http://example.com/upload
# Expected response: HTTP/1.1 413 Payload Too Large

PHP cURL Request and Response Example

PHP script using cURL to handle a 413 Payload Too Large response:

<?php
$fileData = file_get_contents('largefile.txt');
$ch = curl_init('http://example.com/upload');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fileData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 413) {
    echo "Payload too large.";
}
curl_close($ch);
?>

Python Request and Response Example

Python script to send a POST request with a large payload and handle a 413 response:

import requests
with open('largefile.txt', 'rb') as file:
    response = requests.post('http://example.com/upload', data=file)
if response.status_code == 413:
    print("Payload too large")

Apache Configuration for HTTP 413 Payload Too Large

Configuring Apache to limit request payload size:

<VirtualHost *:80>
    ServerName example.com
    # LimitRequestBody directive to specify the maximum payload size
    LimitRequestBody 1048576  # 1 MB
</VirtualHost>

NGINX Configuration for HTTP 413 Payload Too Large

Setting up NGINX to restrict request payload size:

server {
    listen 80;
    server_name example.com;
    client_max_body_size 1M;  # Limit payload size to 1 MB
    location /upload {
        # Additional configurations for upload handling
        # ...
    }
}

HTTP 412 Precondition Failed HTTP 414 Request-URI Too Long


 

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.