{"record":{"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":"exception","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/d041d4d0fd48c3f0832084d33be16cf1c4835f85/aiohttp/client_reqrep.py#L1210-L1246","documentation":"Raised by ClientRequest._update_transfer_encoding() when the request both has a 'Transfer-Encoding: chunked' header already and the internal chunked flag is True. The two represent the same encoding, so providing both is contradictory and aiohttp raises ValueError. The check inspects the lower-cased Transfer-Encoding header for the substring 'chunked'.","triggerScenarios":"Setting headers['Transfer-Encoding']='chunked' manually AND triggering chunked mode (e.g. via compress, or chunked=True through internal logic, or passing a streaming body). _update_transfer_encoding sees 'chunked' in the header plus self.chunked True and raises ValueError.","commonSituations":"Manually adding a Transfer-Encoding header while also using a generator/stream body that auto-enables chunking; mixing explicit headers with the high-level chunked/compress APIs; copy-paste from a raw HTTP example.","solutions":["Don't set Transfer-Encoding manually; let aiohttp manage chunked encoding based on the body type.","If you set the header yourself, ensure chunked mode isn't also triggered (avoid compress and streaming generators simultaneously).","Prefer setting Content-Length for fixed-size bodies to avoid chunked entirely.","Remove any manual 'Transfer-Encoding' header before passing a streaming body."],"exampleFix":"# before\nheaders['Transfer-Encoding'] = 'chunked'\nawait session.post(url, data=stream_gen, headers=headers)  # ValueError\n\n# after — let aiohttp decide; remove manual header\nawait session.post(url, data=stream_gen)","handlingStrategy":"validation","validationCode":"def clean_headers_for_chunked(headers, chunked):\n    te = headers.get('Transfer-Encoding', '').lower()\n    if 'chunked' in te and chunked:\n        # let aiohttp own chunked encoding\n        headers.pop('Transfer-Encoding', None)\n    return headers","typeGuard":"def transfer_encoding_is_unambiguous(headers, chunked) -> bool:\n    te = headers.get('Transfer-Encoding', '').lower()\n    return not ('chunked' in te and chunked)","tryCatchPattern":"try:\n    await session.post(url, data=stream, headers=headers)\nexcept ValueError as e:\n    if 'Transfer-Encoding' in str(e):\n        headers = {k:v for k,v in headers.items() if k.lower() != 'transfer-encoding'}\n        await session.post(url, data=stream, headers=headers)\n    else:\n        raise","preventionTips":["Never set Transfer-Encoding manually; let aiohttp derive it from the body type.","For streaming bodies, ensure no stale Transfer-Encoding header remains.","Prefer Content-Length for fixed-size payloads."],"tags":["client","request","http-headers","transfer-encoding","chunked"],"backgroundTag":null,"analyzedSha":"d041d4d0fd48c3f0832084d33be16cf1c4835f85","analyzedAt":"2026-08-11T20:44:15.550Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}