apache/shenyu · warning · IllegalArgumentException

Response body exceeds maximum size of

Error message

Response body exceeds maximum size of %d bytes

What it means

HttpUtils.readLimitedResponseBody guards against consuming an unbounded HTTP response body. Before streaming the body it checks the Content-Length header reported by the OkHttp response and throws IllegalArgumentException if it already exceeds maxBodySize. This is a pre-read fast-fail so an oversized payload is never buffered into memory.

Solutions

  1. Increase the maxBodySize argument passed to the HTTP call to a value that accommodates the real response size.
  2. Check what URL is being requested — a misrouted or wrong endpoint may return a large static file instead of the expected small API response.
  3. If the backend legitimately returns huge bodies, stream/process them directly instead of using the limited reader.
  4. Catch IllegalArgumentException around the call and surface a clear 'response too large' message to the caller.

Example fix

// before
HttpUtils.readLimitedResponseBody(response, 1024); // fails on large responses
// after
HttpUtils.readLimitedResponseBody(response, 10 * 1024 * 1024); // 10 MB cap
Defensive patterns

Strategy: try-catch

Validate before calling

Response resp = client.newCall(request).execute();
long len = resp.body() != null ? resp.body().contentLength() : -1;
if (len > MAX_BODY_SIZE) { throw new IllegalStateException("Response too large: " + len); }

Try / catch

try {
    byte[] body = HttpUtils.readLimitedResponseBody(response, maxBodySize);
} catch (IllegalArgumentException e) {
    log.warn("Response exceeded {} bytes limit", maxBodySize, e);
    return fallbackResult();
}

Prevention

When it happens

Trigger: Calling readLimitedResponseBody with a responseBody whose contentLength() is greater than the maxBodySize argument passed in; e.g. an admin-side HTTP request against a backend that returns a multi-megabyte body while maxBodySize is a few KB.

Common situations: Configuring a small response size limit for health checks or metadata fetches in shenyu-admin, then pointing the request at an endpoint that returns a large JSON/XML document or an unexpected binary file (e.g. hitting a file download URL instead of an API).

Understand the failure class

Background: payload too large / request exceeds maximum size: why libraries cap bytes and how to fix oversize payloads — this error's family across 50 libraries.

Related errors


AI-assisted analysis of apache/shenyu@567142e072 (2026-09-12). Data as JSON: /api/errors/25c16683f7d6f63c. Report an issue: GitHub.

Appendix: source

Thrown at shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/HttpUtils.java:485

     * Read response body with a size limit to prevent excessive memory usage.
     *
     * @param responseBody the response body to read
     * @param maxBodySize  maximum allowed body size in bytes
     * @return the response body as a string
     * @throws IOException              if an I/O error occurs
     * @throws IllegalArgumentException if the body exceeds maxBodySize
     */
    public static String readLimitedResponseBody(final ResponseBody responseBody, final long maxBodySize) throws IOException {
        if (Objects.isNull(responseBody)) {
            throw new IllegalArgumentException("Response body is empty");
        }
        if (maxBodySize < 0) {
            throw new IllegalArgumentException("Max response body size must not be negative");
        }

        long contentLength = responseBody.contentLength();
        if (contentLength > maxBodySize) {
            throw new IllegalArgumentException(String.format(
                    "Response body exceeds maximum size of %d bytes", maxBodySize));
        }

        ByteArrayOutputStream outputStream = contentLength > 0
                ? new ByteArrayOutputStream((int) Math.min(contentLength, Integer.MAX_VALUE))
                : new ByteArrayOutputStream();
        byte[] buffer = new byte[READ_BUFFER_SIZE];
        long totalBytes = 0;
        try (InputStream inputStream = responseBody.byteStream()) {
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                totalBytes += bytesRead;
                if (totalBytes > maxBodySize) {
                    throw new IllegalArgumentException(String.format(
                            "Response body exceeds maximum size of %d bytes", maxBodySize));
                }
                outputStream.write(buffer, 0, bytesRead);
            }

View on GitHub (pinned to 567142e072)