{"id":"f2285e50c7c3439a","repo":"aio-libs/aiohttp","slug":"chunked-can-not-be-set-if-transfer-encoding-chun","errorCode":null,"errorMessage":"chunked can not be set if \"Transfer-Encoding: chunked\" header is set","messagePattern":"chunked can not be set if \"Transfer-Encoding: chunked\" header is set","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"aiohttp/client_reqrep.py","lineNumber":1228,"sourceCode":"                raise ValueError(\n                    \"compress can not be set if Content-Encoding header is set\"\n                )\n        elif compress:\n            if isinstance(compress, str) and compress not in {\"deflate\", \"gzip\"}:\n                raise ValueError(\n                    \"compress must be one of True, False, 'deflate', or 'gzip'\"\n                )\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 (","sourceCodeStart":1210,"sourceCodeEnd":1246,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/c0ef574e29109210e96e652771ae4e7b88615fa4/aiohttp/client_reqrep.py#L1210-L1246","documentation":"Raised as ValueError in _update_transfer_encoding() when the request already has a 'Transfer-Encoding: chunked' header AND self.chunked is True. Setting chunked twice (via the flag and via the header) is contradictory, so aiohttp rejects it to avoid ambiguous framing.","triggerScenarios":"Fires at line 1226-1231 when 'chunked' is in the lowercase Transfer-Encoding header and self.chunked (the flag) is True. Triggered by passing both chunked=True and headers={'Transfer-Encoding': 'chunked'} to the request.","commonSituations":"Combining manual Transfer-Encoding header with the chunked convenience flag; copying headers verbatim from curl commands while also passing chunked=True.","solutions":["Use only one mechanism: either set the Transfer-Encoding header manually OR pass chunked=True, not both.","If you set Transfer-Encoding: chunked yourself, leave chunked at its default (False).","Remove the manual header when using the chunked flag."],"exampleFix":"# before\nawait session.post(url, data=stream, chunked=True,\n                  headers={'Transfer-Encoding': 'chunked'})\n# after\nawait session.post(url, data=stream, chunked=True)\n# or\nawait session.post(url, data=stream,\n                  headers={'Transfer-Encoding': 'chunked'})","handlingStrategy":"validation","validationCode":"te = headers.get('Transfer-Encoding', '').lower()\nif chunked and 'chunked' in te:\n    chunked = False  # header already conveys intent","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use either the chunked flag or a manual Transfer-Encoding header.","Avoid copy-pasting full header sets from curl.","Validate request-building helpers for conflicting flags."],"tags":["http","headers","transfer-encoding","request-building","configuration"],"analyzedSha":"c0ef574e29109210e96e652771ae4e7b88615fa4","analyzedAt":"2026-08-04T19:51:05.467Z","schemaVersion":2}