{"record":{"id":"f530b5fb68632aec","repo":"OpenFeign/feign","slug":"unable-to-gzip-request-body","errorCode":null,"errorMessage":"Unable to gzip request body","messagePattern":"Unable to gzip request body","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"hc5/src/main/java/feign/hc5/AsyncApacheHttp5Client.java","lineNumber":174,"sourceCode":"      data = gzip(data);\n    } else if (isDeflate && data != null) {\n      data = deflate(data);\n    }\n    if (data != null) {\n      httpRequest.setBody(data, getContentType(request));\n    }\n\n    return httpRequest;\n  }\n\n  private static byte[] gzip(byte[] data) {\n    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();\n        GZIPOutputStream gzip = new GZIPOutputStream(baos)) {\n      gzip.write(data);\n      gzip.finish();\n      return baos.toByteArray();\n    } catch (IOException e) {\n      throw new IllegalStateException(\"Unable to gzip request body\", e);\n    }\n  }\n\n  private static byte[] deflate(byte[] data) {\n    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();\n        DeflaterOutputStream deflater = new DeflaterOutputStream(baos)) {\n      deflater.write(data);\n      deflater.finish();\n      return baos.toByteArray();\n    } catch (IOException e) {\n      throw new IllegalStateException(\"Unable to deflate request body\", e);\n    }\n  }\n\n  private ContentType getContentType(Request request) {\n    ContentType contentType = null;\n    for (final Map.Entry<String, Collection<String>> entry : request.headers().entrySet()) {\n      if (entry.getKey().equalsIgnoreCase(\"Content-Type\")) {","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/OpenFeign/feign/blob/e2a1e27560a1e68840c34f031afca88b36096e30/hc5/src/main/java/feign/hc5/AsyncApacheHttp5Client.java#L156-L192","documentation":"Thrown by AsyncApacheHttp5Client's gzip helper when compressing the request body with GZIPOutputStream raises an IOException. This is a low-level stream failure (memory pressure or stream closure), since gzip of an in-memory byte[] rarely fails. The library wraps it in IllegalStateException so the request fails fast with a clear cause.","triggerScenarios":"Feign request built with header Content-Encoding: gzip while AsyncApacheHttp5Client.toClassicHttpRequest compresses the body; the GZIPOutputStream.write/finish call throws IOException (e.g. body is huge, OOM during stream writes, or ByteArrayOutputStream closed unexpectedly).","commonSituations":"Very large request payloads causing OutOfMemoryError surfaced as stream failure; JVM under extreme memory pressure; custom Request body interception producing odd byte[] states; rarely a JVM/IO subsystem problem.","solutions":["Inspect the wrapped cause (e.getCause()) to find the real IOException; if it is OutOfMemoryError-related, increase heap or reduce body size.","Verify the Content-Encoding header is actually desired — remove it to let the client send uncompressed bodies.","Retry the request once; transient memory pressure is the usual cause.","If gzip is required for huge bodies, stream via a Client implementation that compresses on the fly instead of buffering the whole body."],"exampleFix":"// before\nRequest req = Request.create(...);\nreq.headers().put(\"Content-Encoding\", List.of(\"gzip\")); // compresses full body in memory\n// after\n// drop the header if not required, or ensure adequate heap and smaller body\nMap<String, Collection<String>> headers = new HashMap<>();\nheaders.put(\"Content-Encoding\", List.of(\"gzip\")); // only with adequate -Xmx and bounded body size","handlingStrategy":"try-catch","validationCode":"// before the call\nif (body != null && body.length > 50 * 1024 * 1024) {\n  throw new IllegalArgumentException(\"Body too large for in-memory gzip: \" + body.length);\n}","typeGuard":null,"tryCatchPattern":"try {\n  response = client.execute(request, options);\n} catch (IllegalStateException e) {\n  if (e.getMessage().contains(\"Unable to gzip\")) {\n    Request plain = Request.create(request.httpMethod(), request.url(), withoutContentEncoding(request.headers()), request.body(), request.charset());\n    response = client.execute(plain, options); // fallback uncompressed\n  } else throw e;\n}","preventionTips":["Only set Content-Encoding: gzip when the server requires it","Keep request bodies within memory-friendly sizes","Monitor heap usage for large-payload services"],"tags":["gzip","request-body","http-client","io"],"backgroundTag":"compression-failed","analyzedSha":"e2a1e27560a1e68840c34f031afca88b36096e30","analyzedAt":"2026-09-10T12:37:37.238Z","contentChangedAt":"2026-09-10T12:37:37.238Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}