apache/shenyu · warning · IllegalArgumentException

Response body is empty

Error message

Response body is empty

What it means

HttpUtils.readLimitedResponseBody requires a non-null OkHttp ResponseBody; passing null throws IllegalArgumentException('Response body is empty'). This guard exists because callers that read error/stream bodies may receive a null body (e.g. 204/205 responses or closed responses).

Solutions

  1. Check response.body() for null before calling readLimitedResponseBody, and return a default/empty string instead
  2. Check the HTTP status code first; skip body reading for 204/205
  3. If the body was already consumed, re-issue the request rather than re-reading
  4. Add unit coverage for bodyless responses in code paths using this helper

Example fix

// before
String body = HttpUtils.readLimitedResponseBody(response.body(), MAX);
// after
ResponseBody rb = response.body();
String body = (rb != null) ? HttpUtils.readLimitedResponseBody(rb, MAX) : "";
Defensive patterns

Strategy: type-guard

Validate before calling

if (response.body() == null) { return ""; }

Type guard

boolean hasBody(okhttp3.Response r) { return r.body() != null; }

Try / catch

try { body = HttpUtils.readLimitedResponseBody(rb, max); } catch (IllegalArgumentException e) { if ("Response body is empty".equals(e.getMessage())) { body = ""; } }

Prevention

When it happens

Trigger: Calling HttpUtils.readLimitedResponseBody(response.body(), max) with response.body() == null — typically after executing an OkHttp request that returned a bodyless response (204 No Content, 205 Reset Content) or after the body was already consumed/closed.

Common situations: HTTP long-polling or health-check code hitting endpoints returning 204; calling body() after a previous read; misconfigured server returning empty responses to admin callbacks.

Related errors


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

Appendix: source

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

         * @return String
         */
        public String value() {
            return this.name();
        }
    }

    /**
     * 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;

View on GitHub (pinned to 567142e072)