OpenFeign/feign · error · IllegalStateException

Unable to gzip request body

Error message

Unable to gzip request body

What it means

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.

Solutions

  1. Inspect the wrapped cause (e.getCause()) to find the real IOException; if it is OutOfMemoryError-related, increase heap or reduce body size.
  2. Verify the Content-Encoding header is actually desired — remove it to let the client send uncompressed bodies.
  3. Retry the request once; transient memory pressure is the usual cause.
  4. If gzip is required for huge bodies, stream via a Client implementation that compresses on the fly instead of buffering the whole body.

Example fix

// before
Request req = Request.create(...);
req.headers().put("Content-Encoding", List.of("gzip")); // compresses full body in memory
// after
// drop the header if not required, or ensure adequate heap and smaller body
Map<String, Collection<String>> headers = new HashMap<>();
headers.put("Content-Encoding", List.of("gzip")); // only with adequate -Xmx and bounded body size
Defensive patterns

Strategy: try-catch

Validate before calling

// before the call
if (body != null && body.length > 50 * 1024 * 1024) {
  throw new IllegalArgumentException("Body too large for in-memory gzip: " + body.length);
}

Try / catch

try {
  response = client.execute(request, options);
} catch (IllegalStateException e) {
  if (e.getMessage().contains("Unable to gzip")) {
    Request plain = Request.create(request.httpMethod(), request.url(), withoutContentEncoding(request.headers()), request.body(), request.charset());
    response = client.execute(plain, options); // fallback uncompressed
  } else throw e;
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of OpenFeign/feign@e2a1e27560 (2026-09-10). Data as JSON: /api/errors/f530b5fb68632aec. Report an issue: GitHub.

Appendix: source

Thrown at hc5/src/main/java/feign/hc5/AsyncApacheHttp5Client.java:174

      data = gzip(data);
    } else if (isDeflate && data != null) {
      data = deflate(data);
    }
    if (data != null) {
      httpRequest.setBody(data, getContentType(request));
    }

    return httpRequest;
  }

  private static byte[] gzip(byte[] data) {
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(baos)) {
      gzip.write(data);
      gzip.finish();
      return baos.toByteArray();
    } catch (IOException e) {
      throw new IllegalStateException("Unable to gzip request body", e);
    }
  }

  private static byte[] deflate(byte[] data) {
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DeflaterOutputStream deflater = new DeflaterOutputStream(baos)) {
      deflater.write(data);
      deflater.finish();
      return baos.toByteArray();
    } catch (IOException e) {
      throw new IllegalStateException("Unable to deflate request body", e);
    }
  }

  private ContentType getContentType(Request request) {
    ContentType contentType = null;
    for (final Map.Entry<String, Collection<String>> entry : request.headers().entrySet()) {
      if (entry.getKey().equalsIgnoreCase("Content-Type")) {

View on GitHub (pinned to e2a1e27560)