{"id":"bd5ca33822f6fcc7","repo":"aio-libs/aiohttp","slug":"compress-can-not-be-set-if-content-encoding-header","errorCode":null,"errorMessage":"compress can not be set if Content-Encoding header is set","messagePattern":"compress can not be set if Content-Encoding header is set","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"aiohttp/client_reqrep.py","lineNumber":1210,"sourceCode":"            del self.headers[hdrs.COOKIE]\n\n        for name, value in cookies.items():\n            # Use helper to preserve coded_value exactly as sent by server\n            c[name] = preserve_morsel_with_coded_value(value)\n\n        self.headers[hdrs.COOKIE] = c.output(header=\"\", sep=\";\").strip()\n\n    def _update_content_encoding(\n        self, data: Any, compress: bool | Literal[\"deflate\", \"gzip\"]\n    ) -> None:\n        \"\"\"Set request content encoding.\"\"\"\n        self.compress = None\n        if not data:\n            return\n\n        if self.headers.get(hdrs.CONTENT_ENCODING):\n            if compress:\n                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(","sourceCodeStart":1192,"sourceCodeEnd":1228,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/c0ef574e29109210e96e652771ae4e7b88615fa4/aiohttp/client_reqrep.py#L1192-L1228","documentation":"Raised as ValueError in ClientRequest._update_content_encoding() when the caller passes a truthy `compress` argument while a Content-Encoding header is already set on the request. Setting both would create conflicting/duplicate compression directives, so aiohttp refuses rather than silently override the header.","triggerScenarios":"Fires at line 1208-1212 when headers already contains Content-Encoding AND the compress argument is truthy. Triggered by session.post(url, data=..., compress=True, headers={'Content-Encoding': 'gzip'}).","commonSituations":"Combining manual Content-Encoding headers with the compress convenience flag; copy-pasting headers from another request while also enabling compress.","solutions":["Choose one approach: either set Content-Encoding manually OR use compress=True, not both.","If you set Content-Encoding yourself, pass compress=False (the default) and ensure the body is already encoded.","Remove the manual Content-Encoding header before enabling compress."],"exampleFix":"# before\nawait session.post(url, data=payload, compress=True,\n                  headers={'Content-Encoding': 'gzip'})\n# after - pick one\nawait session.post(url, data=payload, compress=True)  # let aiohttp encode\n# or\nawait session.post(url, data=gzip_bytes,\n                  headers={'Content-Encoding': 'gzip'})  # pre-encoded","handlingStrategy":"validation","validationCode":"if compress and 'Content-Encoding' in headers:\n    del headers['Content-Encoding']  # or set compress=False","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pick one compression mechanism: the compress flag OR a manual header.","Document in helper functions which approach is used.","Reject configs that set both at once during validation."],"tags":["http","headers","compression","request-building","configuration"],"analyzedSha":"c0ef574e29109210e96e652771ae4e7b88615fa4","analyzedAt":"2026-08-04T19:51:05.467Z","schemaVersion":2}