{"record":{"id":"de6365bac25d2740","repo":"aio-libs/aiohttp","slug":"body-and-text-are-not-allowed-together","errorCode":null,"errorMessage":"body and text are not allowed together","messagePattern":"body and text are not allowed together","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"aiohttp/web_response.py","lineNumber":555,"sourceCode":"\n    _compressed_body: bytes | None = None\n    _send_headers_immediately = False\n\n    def __init__(\n        self,\n        *,\n        body: Any = None,\n        status: int = 200,\n        reason: str | None = None,\n        text: str | None = None,\n        headers: LooseHeaders | None = None,\n        content_type: str | None = None,\n        charset: str | None = None,\n        zlib_executor_size: int = MAX_SYNC_CHUNK_SIZE,\n        zlib_executor: Executor | None = None,\n    ) -> None:\n        if body is not None and text is not None:\n            raise ValueError(\"body and text are not allowed together\")\n\n        if headers is None:\n            real_headers: CIMultiDict[str] = CIMultiDict()\n        else:\n            real_headers = CIMultiDict(headers)\n\n        if content_type is not None and \"charset\" in content_type:\n            raise ValueError(\"charset must not be in content_type argument\")\n\n        if text is not None:\n            if hdrs.CONTENT_TYPE in real_headers:\n                if content_type or charset:\n                    raise ValueError(\n                        \"passing both Content-Type header and \"\n                        \"content_type or charset params \"\n                        \"is forbidden\"\n                    )\n            else:","sourceCodeStart":537,"sourceCodeEnd":573,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/d041d4d0fd48c3f0832084d33be16cf1c4835f85/aiohttp/web_response.py#L537-L573","documentation":"Raised by the Response constructor when both `body` and `text` kwargs are passed. The two are alternative ways to supply the response payload: `text` is encoded to bytes using the charset, while `body` is raw bytes. Passing both is ambiguous (which one wins?) so aiohttp rejects it.","triggerScenarios":"Constructing `Response(body=b'data', text='data')`, or a helper that conditionally sets both and forwards them. Also `Response(text=text, body=payload)` when both branches of a conditional populate their variable.","commonSituations":"Refactoring that left both arguments wired in; helper functions with `body=None, text=None` defaults where the caller passes both; building an error response that mistakenly forwards both the rendered HTML and a pre-encoded bytes copy.","solutions":["Pass exactly one of `body=` or `text=`. Use `text=` for str payloads (it sets content_type/charset), `body=` for pre-encoded bytes.","In helper functions, normalize to one channel: `if text is not None: body = text.encode(charset); text = None`.","Use `json_response(data=...)` for JSON, which encodes for you and accepts only one source.","Add an assertion in your wrappers: `assert (body is None) or (text is None)`."],"exampleFix":"// before\nreturn Response(body=content.encode(), text=content)\n\n// after\nreturn Response(text=content)  # let aiohttp encode with charset","handlingStrategy":"validation","validationCode":"def make_response(*, body=None, text=None, **kw):\n    assert body is None or text is None, 'pass only one of body or text'\n    return Response(body=body, text=text, **kw)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass exactly one of body= or text=.","Normalize helpers to a single channel before constructing Response.","Use json_response() for JSON payloads.","Add assertions in wrappers to catch double-assignment early."],"tags":["aiohttp","web-response","constructor","validation","body"],"backgroundTag":null,"analyzedSha":"d041d4d0fd48c3f0832084d33be16cf1c4835f85","analyzedAt":"2026-08-11T20:44:15.550Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}