{"id":"b2933ff3f45a6b85","repo":"aio-libs/aiohttp","slug":"using-chunked-encoding-is-forbidden-for-http-majo","errorCode":null,"errorMessage":"Using chunked encoding is forbidden for HTTP/{major}.{minor}","messagePattern":"Using chunked encoding is forbidden for HTTP/(.+?)\\.(.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"aiohttp/web_response.py","lineNumber":393,"sourceCode":"        writer = self._payload_writer\n        assert writer is not None\n        keep_alive = self._keep_alive\n        if keep_alive is None:\n            keep_alive = request.keep_alive\n        self._keep_alive = keep_alive\n\n        version = request.version\n\n        headers = self._headers\n        if self._cookies:\n            populate_with_cookies(headers, self._cookies)\n\n        if self._compression:\n            await self._start_compression(request)\n\n        if self._chunked:\n            if version != HttpVersion11:\n                raise RuntimeError(\n                    \"Using chunked encoding is forbidden \"\n                    f\"for HTTP/{request.version.major}.{request.version.minor}\"\n                )\n            if not self._must_be_empty_body:\n                writer.enable_chunking()\n                headers[hdrs.TRANSFER_ENCODING] = \"chunked\"\n        elif self._length_check:  # Disabled for WebSockets\n            writer.length = self.content_length\n            if writer.length is None:\n                if version >= HttpVersion11:\n                    if not self._must_be_empty_body:\n                        writer.enable_chunking()\n                        headers[hdrs.TRANSFER_ENCODING] = \"chunked\"\n                elif not self._must_be_empty_body:\n                    keep_alive = False\n\n        # HTTP 1.1: https://tools.ietf.org/html/rfc7230#section-3.3.2\n        # HTTP 1.0: https://tools.ietf.org/html/rfc1945#section-10.4","sourceCodeStart":375,"sourceCodeEnd":411,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/c0ef574e29109210e96e652771ae4e7b88615fa4/aiohttp/web_response.py#L375-L411","documentation":"Chunked transfer-encoding is only valid for HTTP/1.1. If you enable chunked via enable_chunked_encoding() but the client negotiated HTTP/1.0, _prepare_headers raises RuntimeError at line 392-396 rather than emitting an invalid response.","triggerScenarios":"Calling resp.enable_chunked_encoding() and serving a client that sent an HTTP/1.0 request line (request.version != HttpVersion11). The version comes from the parsed request, so this depends on what the client sent, not on server config.","commonSituations":"Testing against an HTTP/1.0-only client or proxy (curl --http1.0, legacy tools); an upstream proxy downgrading the protocol; forcing chunked for streaming then hitting an old client.","solutions":["Don't force chunked; let aiohttp auto-decide based on request version (it falls back to closing the connection for HTTP/1.0 at lines 407-408).","Require HTTP/1.1 from clients if you must use chunked streaming.","Remove the resp.enable_chunked_encoding() call entirely."],"exampleFix":"# before\nresp = StreamResponse()\nresp.enable_chunked_encoding()  # breaks HTTP/1.0 clients\n\n# after\nresp = StreamResponse()  # aiohttp auto-enables chunked for 1.1, closes conn for 1.0","handlingStrategy":"validation","validationCode":"# Don't force chunked; let aiohttp decide. If you must, gate on request version:\nfrom aiohttp import HttpVersion11\n\nif request.version == HttpVersion11:\n    resp.enable_chunked_encoding()","typeGuard":null,"tryCatchPattern":"try:\n    await resp.prepare(request)\nexcept RuntimeError as e:\n    if 'chunked encoding is forbidden' in str(e):\n        # client is HTTP/1.0; fall back to connection-close streaming\n        ...","preventionTips":["Avoid resp.enable_chunked_encoding() unless you control client protocol versions.","Document that your endpoint requires HTTP/1.1 if you use chunked.","Test against HTTP/1.0 clients (curl --http1.0) during development."],"tags":["http","chunked","transfer-encoding","protocol","response"],"analyzedSha":"c0ef574e29109210e96e652771ae4e7b88615fa4","analyzedAt":"2026-08-04T19:51:05.467Z","schemaVersion":2}