ThisCalculator

Everyday

Why a Short API Payload Can Fail a 21 KB Limit

Updated · Standard formula, shown below

This article explains why a small API request or response can be rejected even when the server advertises a 21 KB payload limit. The short answer is that the 21 KB limit usually applies to the entire HTTP message—headers, URL, cookies, and body—not just the JSON or XML payload you see in your code, so a tiny body can still exceed the cap once overhead is counted.

Background: Where the 21 KB Limit Comes From

Many web servers and API gateways impose a maximum request size to protect against abuse and memory exhaustion. The exact number—21 KB—is not a universal standard; it appears in some older server configurations and reverse proxy defaults, and it is often a leftover from a time when 8 KB or 16 KB was common. For example, some versions of Nginx historically defaulted client_max_body_size to 1 MB, but a proxy or application framework might set a lower value. The 21 KB figure could be a custom setting from a specific platform or a misinterpretation of a 21,000-byte limit.

What matters is that the limit is not a limit on your payload alone. It is a limit on the whole HTTP message. When you send a request, the server reads the request line, headers, and body as one stream. If the total exceeds the configured maximum, the server returns an error—often 413 Payload Too Large—even if the body itself is only a few hundred bytes.

How the HTTP Message Is Constructed

Every HTTP request has three main parts:

  • Request line: the method (GET, POST, etc.), the path, and the HTTP version. A long URL can add hundreds of bytes.
  • Headers: each header like Content-Type, Authorization, Cookie, or custom headers adds its name, value, and a CRLF. A single cookie header can easily be 1–2 KB if the site sets many cookies.
  • Body: the actual payload, such as JSON or form data. This is what you usually think of as the “payload.”

For a response, the same idea applies: status line, headers, and body. The server counts the entire response when enforcing its output limit.

Here is a concrete example. Suppose you POST a JSON body that is exactly 500 bytes:

{"user":"alice","action":"update","items":[1,2,3]}

Your request might look like this:

POST /api/update HTTP/1.1
Host: example.com
Content-Type: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
Cookie: session=abc123; theme=dark; lang=en

{the 500-byte body}

The Authorization header alone is about 200 bytes, the Cookie header about 60, and the other headers add another 100. The total easily reaches 900–1000 bytes. If the limit is 21,000 bytes, you are still fine. But if the limit is actually 1 KB (1024 bytes), you are over. Many developers see “21 KB” and assume they can send 21,000 bytes of JSON, but the real limit is the whole message.

Why the Limit Is Often Confusing

Part of the confusion comes from documentation. API docs often say “max payload size: 21 KB” without specifying that it includes headers. Another source of confusion is that different layers have different limits:

  • Web server (e.g., Nginx, Apache) may have its own limit.
  • Application framework (e.g., Express, Django, Spring) may have a separate limit.
  • API gateway (e.g., Kong, AWS API Gateway) may impose yet another.

If any layer rejects the message, you get an error. A request that passes the web server might fail at the application layer because the framework’s limit is lower. Conversely, a request that passes the app might fail at the gateway if the gateway counts the body after decompression or after transformation.

Another common gotcha is encoding. If you send data with Content-Encoding: gzip, the server may count the compressed size or the uncompressed size depending on configuration. Most servers count the decompressed body size to prevent decompression bombs, so a small compressed payload can expand to a huge size and trip the limit.

How to Diagnose the Exact Cause

When you get a 413 error, the first step is to check the server logs. They often show the exact limit and the size of the request that was rejected. If you cannot access logs, you can measure the request size yourself:

  1. Use a tool like curl -v to see the full request headers and body.
  2. Count the bytes of the entire request, including the request line, each header, the blank line, and the body. Tools like wc -c on a file containing the raw request can help.
  3. Compare that total to the documented limit. If it is close, you have found the issue.
  4. If the total is well under the limit, the problem might be at a different layer—check the application framework’s configuration and any proxy in between.

Also, check if the limit is in bytes or kilobytes. Some systems use 1 KB = 1024 bytes, others use 1000. A “21 KB” limit could be 21,000 or 21,504 bytes. The difference is small but can matter if you are right at the edge.

Common Misconceptions and Mistakes

  • “The payload is only 2 KB, so it should pass.” That ignores headers and the request line, which can add several KB.
  • “The limit is on the JSON body only.” In most servers, it is on the entire message.
  • “I removed the body, so the request is tiny.” A GET request with a long query string can still be large. A URL with many parameters can be several KB.
  • “I compressed the body, so it is small.” The server may count the uncompressed size.
  • “The limit is per field.” Some frameworks have per-field limits (e.g., a maximum string length), but that is separate from the total message limit.

One real-world example: a developer sends a JSON payload of 20 KB, but the server limit is 21 KB. The request includes a 2 KB authentication token in the header, pushing the total to 22 KB, and it fails. The fix is not to shrink the payload but to reduce header size, e.g., by using a shorter token or moving some data to the body.

How to Fix or Work Around the Limit

If you control the server, you can increase the limit. In Nginx, set client_max_body_size in the server or location block. In Apache, use LimitRequestBody. In Express, use express.json({limit: '50mb'}). But you should not blindly increase the limit; it exists for a reason.

If you do not control the server, you have several options:

  • Reduce header size: remove unnecessary cookies, use shorter tokens, or move data from headers to the body.
  • Compress the body with gzip, but only if the server counts the compressed size (check documentation).
  • Split the request into multiple smaller requests, e.g., upload data in chunks.
  • Use a different endpoint that accepts multipart uploads, which may have a different limit.
  • Send data via a file upload if the limit is higher for files.

If you are hitting the limit on a response, you can paginate the data or request only the fields you need.

Frequently Asked Questions

Why does my 5 KB JSON fail when the limit is 21 KB?

Because the limit usually applies to the whole HTTP message, not just the JSON. Headers, cookies, and the request line can add several kilobytes. Also, the limit might be in kibibytes (21,504 bytes) or the server might count the decompressed size if you use compression.

What does 413 Payload Too Large mean?

It means the server refused the request because the total size of the request (headers + body) exceeded the maximum allowed. The server should include the limit in the response or logs.

Can I see the exact size of my HTTP request?

Yes. Use curl -v to print the request headers, then copy the entire request (including headers and body) into a file and run wc -c on it. Or use browser developer tools: the Network tab shows the request headers and payload size, but it may not show the total bytes sent on the wire.

Is the 21 KB limit the same everywhere?

No. It is a configuration choice. Different servers, frameworks, and gateways have different defaults. Always check the documentation for the specific system you are using.

How do I increase the limit in my own server?

It depends on the server. For Nginx, add client_max_body_size 50m; in the server block. For Apache, use LimitRequestBody 52428800. For Express, use express.json({limit: '50mb'}). After changing, restart the service.

Does the limit apply to the response as well?

Yes, many servers also limit the response size. If you are sending a large response, you might get a 500 error or the connection might be cut. Check your server logs for the specific error.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.