{"id":"f9d77632386f4a95","repo":"aio-libs/aiohttp","slug":"only-one-of-data-text-or-body-should-be-specifie","errorCode":null,"errorMessage":"only one of data, text, or body should be specified","messagePattern":"only one of data, text, or body should be specified","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"aiohttp/web_response.py","lineNumber":752,"sourceCode":"        )\n        self._headers[hdrs.CONTENT_ENCODING] = coding.value\n        self._headers[hdrs.CONTENT_LENGTH] = str(len(self._compressed_body))\n\n\ndef json_response(\n    data: Any = sentinel,\n    *,\n    text: str | None = None,\n    body: bytes | None = None,\n    status: int = 200,\n    reason: str | None = None,\n    headers: LooseHeaders | None = None,\n    content_type: str = \"application/json\",\n    dumps: JSONEncoder = json.dumps,\n) -> Response:\n    if data is not sentinel:\n        if text or body:\n            raise ValueError(\"only one of data, text, or body should be specified\")\n        else:\n            text = dumps(data)\n    return Response(\n        text=text,\n        body=body,\n        status=status,\n        reason=reason,\n        headers=headers,\n        content_type=content_type,\n    )\n\n\ndef json_bytes_response(\n    data: Any = sentinel,\n    *,\n    dumps: JSONBytesEncoder,\n    body: bytes | None = None,\n    status: int = 200,","sourceCodeStart":734,"sourceCodeEnd":770,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/c0ef574e29109210e96e652771ae4e7b88615fa4/aiohttp/web_response.py#L734-L770","documentation":"json_response() encodes `data` into text via dumps(). If you also pass text= or body=, the two are contradictory — data already produces the text. The guard at lines 750-752 rejects it.","triggerScenarios":"Calling json_response(data=obj, text='...') or json_response(data=obj, body=b'...'). `data` defaults to a sentinel so it's only 'set' when explicitly passed.","commonSituations":"Wrapping json_response and forwarding both a pre-serialized string and the object; copy-paste leaving stale kwargs.","solutions":["Pass only data= and let dumps() serialize it.","If you already have a JSON string, pass text= and omit data=.","If you already have bytes, use Response(body=..., content_type='application/json')."],"exampleFix":"# before\nresp = json_response(data=obj, text=json.dumps(obj))  # raises ValueError\n\n# after\nresp = json_response(data=obj)","handlingStrategy":"validation","validationCode":"def safe_json_response(data=sentinel, *, text=None, body=None, **kw):\n    if data is not sentinel and (text or body):\n        raise ValueError('pass only one of data, text, or body')\n    from aiohttp import json_response\n    return json_response(data=data, text=text, body=body, **kw)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass only data= to json_response; let dumps() serialize.","For pre-serialized strings use text= and omit data=.","For pre-serialized bytes use Response(body=..., content_type='application/json')."],"tags":["http","json","response","validation"],"analyzedSha":"c0ef574e29109210e96e652771ae4e7b88615fa4","analyzedAt":"2026-08-04T19:51:05.467Z","schemaVersion":2}