The HTTP 413 status code stands for "Payload Too Large". It is a client error response indicating that the request is larger than the server is willing or able to process. This often occurs when the size of the request body (such as a file upload or a large POST request) exceeds the server's configured maximum allowed size for incoming requests.

When is HTTP 413 Used?

  • When the client sends a request with a body that is too large for the server to handle.
  • Typically occurs with large file uploads, data-intensive POST requests, or any request where the body size exceeds the server's configured limits (e.g., maximum upload size).
  • It can also happen when the client tries to send a very large amount of data in a single request (e.g., a bulk insert in a database API).

Common Causes of HTTP 413 Errors

  • Large file uploads exceeding the allowed size limit.
  • Massive JSON payloads in API requests that exceed the configured limit.
  • Requests containing large data (e.g., images, videos, or binary files) that the server is not configured to accept.

Example Scenarios

  • Uploading a file larger than the configured maximum upload size for a server.
  • Sending a large JSON object or a bulk data submission to an API where the server has size limitations.
  • Attempting to POST a large form with many fields or extensive file attachments.

Example

Client Request (Uploading a Large File)

    
        POST /upload HTTP/1.1
        Host: example.com
        Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
        Content-Length: 50000000

        ------WebKitFormBoundary7MA4YWxkTrZu0gW
        Content-Disposition: form-data; name="file"; filename="large-file.txt"
        Content-Type: text/plain

        <file content here>
        ------WebKitFormBoundary7MA4YWxkTrZu0gW--
    

(The client attempts to upload a large file, but the size exceeds the server’s allowed limit.)

Server Response (413 Payload Too Large)

    
        HTTP/1.1 413 Payload Too Large
        Content-Type: application/json

        {
          "error": "The uploaded file is too large. Please reduce the size and try again."
        }
    

Best Practices for Handling HTTP 413

  • Configure server limits: Ensure that server-side limits for request bodies are set appropriately, and adjust them to accommodate legitimate use cases without overloading the system.
  • Provide clear error messages: Include helpful instructions in the response (e.g., "File size limit is 10MB").
  • Allow chunked uploads: For large files, consider implementing chunked uploads to allow large files to be uploaded in smaller, more manageable pieces.
  • Client-side validation: Implement client-side checks to validate file sizes before submitting requests to the server, which can prevent unnecessary requests and improve the user experience.

Summary

HTTP 413 means that the request payload is too large for the server to handle, often due to large file uploads or excessively large data submissions. To resolve this error, the client needs to either reduce the size of the request body or the server needs to be configured to accept larger payloads.