{"id":"dff2f9c814d57b10","repo":"aio-libs/aiohttp","slug":"can-not-decode-content-encoding-s","errorCode":null,"errorMessage":"Can not decode content-encoding: %s","messagePattern":"Can not decode content-encoding: (.+?)","errorType":"exception","errorClass":"ContentEncodingError","httpStatus":400,"severity":"error","filePath":"aiohttp/http_parser.py","lineNumber":1189,"sourceCode":"            # RFC1950\n            # bits 0..3 = CM = 0b1000 = 8 = \"deflate\"\n            # bits 4..7 = CINFO = 1..7 = windows size.\n            if self.encoding == \"deflate\" and chunk[0] & 0xF != 8:\n                # Change the decoder to decompress incorrectly compressed data\n                # Actually we should issue a warning about non-RFC-compliant data.\n                self.decompressor = ZLibDecompressor(\n                    encoding=self.encoding, suppress_deflate_header=True\n                )\n            self._started_decoding = True\n\n        low_water = self.out._low_water\n        max_length = (\n            0 if low_water >= sys.maxsize else max(self._max_decompress_size, low_water)\n        )\n        try:\n            chunk = self.decompressor.decompress_sync(chunk, max_length=max_length)\n        except Exception:\n            raise ContentEncodingError(\n                \"Can not decode content-encoding: %s\" % self.encoding\n            )\n\n        if chunk:\n            self.out.feed_data(chunk)\n        return self.decompressor.data_available\n\n    def feed_eof(self) -> None:\n        chunk = self.decompressor.flush()\n        # This should never contain data as we defer the call until exhausting\n        # the decompression. If .flush() is returning data, this may indicate a\n        # zip bomb vulnerability as it will decompress all remaining data at once.\n        assert not chunk\n\n        if self.size > 0:\n            # decompressor is not brotli unless encoding is \"br\"\n            if self.encoding == \"deflate\" and not self.decompressor.eof:  # type: ignore[union-attr]\n                raise ContentEncodingError(\"deflate\")","sourceCodeStart":1171,"sourceCodeEnd":1207,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/c0ef574e29109210e96e652771ae4e7b88615fa4/aiohttp/http_parser.py#L1171-L1207","documentation":"Generic ContentEncodingError raised when the underlying decompressor (zlib/brotli/zstd) raises during decompress_sync — i.e. the compressed body is corrupt, truncated, or uses an unexpected format. The encoding name is interpolated into the message.","triggerScenarios":"DeflateBuffer.feed_data calls self.decompressor.decompress_sync(chunk) which raises (zlib.error, brotli error, zstd error); the exception is caught and re-raised as ContentEncodingError with the encoding. Happens on corrupt gzip/deflate/br/zstd payloads.","commonSituations":"Corrupt downloads; compression applied twice; Content-Encoding mismatch (server says gzip but sends raw); truncated compressed stream; intermediary altering the body.","solutions":["Verify the body is actually encoded as advertised (curl --compressed -o /tmp/x and inspect).","Check for a proxy/CDN re-compressing or stripping encoding.","Retry; if stable, report to the upstream.","As a workaround, request no compression (Accept-Encoding: identity) or set auto_decompress=False."],"exampleFix":"// before\n#   body = await resp.read()  # zlib.error on corrupt gzip\n\n# after - request identity or stream raw\nheaders = {'Accept-Encoding': 'identity'}\nresp = await session.get(url, headers=headers)\n# or disable auto-decompress and handle manually\nsession = aiohttp.ClientSession(auto_decompress=False)","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"from aiohttp.http_exceptions import ContentEncodingError\ntry:\n    body = await resp.read()\nexcept ContentEncodingError:\n    # retry without compression\n    async with session.get(url, headers={'Accept-Encoding': 'identity'}) as r:\n        body = await r.read()","preventionTips":["Validate Content-Encoding matches the actual bytes.","Handle decompression errors as transport failures with retry."],"tags":["http","compression","decode-error","response","corruption"],"analyzedSha":"c0ef574e29109210e96e652771ae4e7b88615fa4","analyzedAt":"2026-08-04T19:51:05.467Z","schemaVersion":2}