{"record":{"id":"68b7e43568574d18","repo":"apache/shenyu","slug":"response-body-is-empty","errorCode":null,"errorMessage":"Response body is empty","messagePattern":"Response body is empty","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":400,"severity":"warning","filePath":"shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/HttpUtils.java","lineNumber":477,"sourceCode":"         * @return String\n         */\n        public String value() {\n            return this.name();\n        }\n    }\n\n    /**\n     * Read response body with a size limit to prevent excessive memory usage.\n     *\n     * @param responseBody the response body to read\n     * @param maxBodySize  maximum allowed body size in bytes\n     * @return the response body as a string\n     * @throws IOException              if an I/O error occurs\n     * @throws IllegalArgumentException if the body exceeds maxBodySize\n     */\n    public static String readLimitedResponseBody(final ResponseBody responseBody, final long maxBodySize) throws IOException {\n        if (Objects.isNull(responseBody)) {\n            throw new IllegalArgumentException(\"Response body is empty\");\n        }\n        if (maxBodySize < 0) {\n            throw new IllegalArgumentException(\"Max response body size must not be negative\");\n        }\n\n        long contentLength = responseBody.contentLength();\n        if (contentLength > maxBodySize) {\n            throw new IllegalArgumentException(String.format(\n                    \"Response body exceeds maximum size of %d bytes\", maxBodySize));\n        }\n\n        ByteArrayOutputStream outputStream = contentLength > 0\n                ? new ByteArrayOutputStream((int) Math.min(contentLength, Integer.MAX_VALUE))\n                : new ByteArrayOutputStream();\n        byte[] buffer = new byte[READ_BUFFER_SIZE];\n        long totalBytes = 0;\n        try (InputStream inputStream = responseBody.byteStream()) {\n            int bytesRead;","sourceCodeStart":459,"sourceCodeEnd":495,"githubUrl":"https://github.com/apache/shenyu/blob/567142e07261b3e615ae8850b30f4421f455cc5d/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/HttpUtils.java#L459-L495","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check response.body() for null before calling readLimitedResponseBody, and return a default/empty string instead","Check the HTTP status code first; skip body reading for 204/205","If the body was already consumed, re-issue the request rather than re-reading","Add unit coverage for bodyless responses in code paths using this helper"],"exampleFix":"// before\nString body = HttpUtils.readLimitedResponseBody(response.body(), MAX);\n// after\nResponseBody rb = response.body();\nString body = (rb != null) ? HttpUtils.readLimitedResponseBody(rb, MAX) : \"\";","handlingStrategy":"type-guard","validationCode":"if (response.body() == null) { return \"\"; }","typeGuard":"boolean hasBody(okhttp3.Response r) { return r.body() != null; }","tryCatchPattern":"try { body = HttpUtils.readLimitedResponseBody(rb, max); } catch (IllegalArgumentException e) { if (\"Response body is empty\".equals(e.getMessage())) { body = \"\"; } }","preventionTips":["Treat 204/205 responses as bodyless and skip reading","Never read response.body() twice; OkHttp bodies are one-shot","Null-check response.body() at every call site of this helper"],"tags":["http","okhttp","response-body","null-check"],"backgroundTag":"empty-response-body","analyzedSha":"567142e07261b3e615ae8850b30f4421f455cc5d","analyzedAt":"2026-09-12T10:08:21.293Z","contentChangedAt":"2026-09-12T10:08:21.293Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}