{"id":"199e512a479f3673","repo":"aio-libs/aiohttp","slug":"chunked-can-not-be-set-if-content-length-header-is","errorCode":null,"errorMessage":"chunked can not be set if Content-Length header is set","messagePattern":"chunked can not be set if Content-Length header is set","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"aiohttp/client_reqrep.py","lineNumber":1235,"sourceCode":"                )\n            self.compress = compress if isinstance(compress, str) else \"deflate\"\n            self.headers[hdrs.CONTENT_ENCODING] = self.compress\n            self.chunked = True  # enable chunked, no need to deal with length\n\n    def _update_transfer_encoding(self) -> None:\n        \"\"\"Analyze transfer-encoding header.\"\"\"\n        te = self.headers.get(hdrs.TRANSFER_ENCODING, \"\").lower()\n\n        if \"chunked\" in te:\n            if self.chunked:\n                raise ValueError(\n                    \"chunked can not be set \"\n                    'if \"Transfer-Encoding: chunked\" header is set'\n                )\n\n        elif self.chunked:\n            if hdrs.CONTENT_LENGTH in self.headers:\n                raise ValueError(\n                    \"chunked can not be set if Content-Length header is set\"\n                )\n\n            self.headers[hdrs.TRANSFER_ENCODING] = \"chunked\"\n\n    def _update_body_from_data(self, body: Any) -> None:\n        \"\"\"Update request body from data.\"\"\"\n        if body is None:\n            self._body = self._EMPTY_BODY\n            # Set Content-Length to 0 when body is None for methods that expect a body\n            if (\n                self.method not in self.GET_METHODS\n                and not self.chunked\n                and hdrs.CONTENT_LENGTH not in self.headers\n            ):\n                self.headers[hdrs.CONTENT_LENGTH] = \"0\"\n            return\n","sourceCodeStart":1217,"sourceCodeEnd":1253,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/c0ef574e29109210e96e652771ae4e7b88615fa4/aiohttp/client_reqrep.py#L1217-L1253","documentation":"Raised as ValueError in _update_transfer_encoding() when self.chunked is True and the request also carries a Content-Length header. HTTP semantics forbid advertising both a fixed length and chunked transfer encoding on the same request; the two framing mechanisms are mutually exclusive.","triggerScenarios":"Fires at line 1233-1237 when chunked flag is True, no Transfer-Encoding header is set, but CONTENT_LENGTH is present in headers. Triggered by passing chunked=True together with a Content-Length header, or when a body sets Content-Length and chunked is forced on.","commonSituations":"Forcing chunked=True for a streaming body while a middleware also sets Content-Length; manually setting Content-Length on a request with a generator/async-generator body (which aiohttp auto-chunks).","solutions":["Do not set Content-Length when using chunked=True or streaming bodies.","Let aiohttp compute framing: it picks Content-Length for fixed bytes and chunked for streams.","Remove any manually-set Content-Length header before sending a streaming body.","Buffer the body into bytes first if you need a fixed Content-Length."],"exampleFix":"# before\nawait session.post(url, data=async_generator(),\n                  chunked=True,\n                  headers={'Content-Length': '1024'})\n# after - let aiohttp decide framing\nawait session.post(url, data=async_generator(), chunked=True)\n# or buffer first\nbody = b''.join([chunk async for chunk in gen])\nawait session.post(url, data=body)  # Content-Length auto-set","handlingStrategy":"validation","validationCode":"if chunked and 'Content-Length' in headers:\n    del headers['Content-Length']  # chunked and length are mutually exclusive","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never set Content-Length on streaming/chunked bodies.","Buffer the body into bytes if you need a fixed length.","Let aiohttp choose framing automatically when possible."],"tags":["http","headers","transfer-encoding","content-length","request-building"],"analyzedSha":"c0ef574e29109210e96e652771ae4e7b88615fa4","analyzedAt":"2026-08-04T19:51:05.467Z","schemaVersion":2}