{"record":{"id":"cb6943a5f34d4233","repo":"aio-libs/aiohttp","slug":"data-and-json-parameters-can-not-be-used-at-the-sa","errorCode":null,"errorMessage":"data and json parameters can not be used at the same time","messagePattern":"data and json parameters can not be used at the same time","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"aiohttp/client.py","lineNumber":516,"sourceCode":"        # NOTE: timeout clamps existing connect and read timeouts.  We cannot\n        # set the default to None because we need to detect if the user wants\n        # to use the existing timeouts by setting timeout to None.\n\n        if self.closed:\n            raise RuntimeError(\"Session is closed\")\n\n        method = method.upper()\n\n        if ssl is sentinel:\n            ssl = self._default_ssl\n        if not isinstance(ssl, SSL_ALLOWED_TYPES):\n            raise TypeError(\n                \"ssl should be SSLContext, Fingerprint, or bool, \"\n                f\"got {ssl!r} instead.\"\n            )\n\n        if data is not None and json is not None:\n            raise ValueError(\n                \"data and json parameters can not be used at the same time\"\n            )\n        elif json is not None:\n            if self._json_serialize_bytes is not None:\n                data = payload.JsonBytesPayload(json, dumps=self._json_serialize_bytes)\n            else:\n                data = payload.JsonPayload(json, dumps=self._json_serialize)\n\n        redirects = 0\n        history: list[ClientResponse] = []\n        version = self._version\n        params = params or {}\n\n        # Merge with default headers and transform to CIMultiDict\n        headers = self._prepare_headers(headers)\n\n        try:\n            url = self._build_url(str_or_url)","sourceCodeStart":498,"sourceCodeEnd":534,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/d041d4d0fd48c3f0832084d33be16cf1c4835f85/aiohttp/client.py#L498-L534","documentation":"Raised by ClientSession.request when both data and json are not None. The two arguments imply mutually exclusive body encodings: data is form/stream-oriented, json is serialized with the configured serializer. Allowing both would leave the actual body undefined.","triggerScenarios":"Calling session.post(url, data=payload, json=obj). Also fires when one of them is set to a falsy-but-not-None value (e.g. {} or []) combined with the other, since the check is 'is not None'.","commonSituations":"Refactoring a call that sent form data and tacking on json without removing data. Helper functions that accept **kwargs and forward both. Copy-pasting snippets that each contribute one of the two kwargs.","solutions":["Pick one body form: json=obj for JSON APIs, data=... for forms or raw bodies.","If you need JSON inside multipart/form fields, build a FormData and embed the JSON as one field instead of using the json kwarg.","Audit wrapper functions to ensure only one of data/json is forwarded."],"exampleFix":"// before\nawait session.post(url, data=form, json={'a': 1})\n// after\nawait session.post(url, json={'a': 1})","handlingStrategy":"validation","validationCode":"def exactly_one_of(**kwargs):\n    present = [k for k, v in kwargs.items() if v is not None]\n    if len(present) > 1:\n        raise ValueError(f'only one of {list(kwargs)} may be set, got {present}')\n\n# usage:\nexactly_one_of(data=data, json=json)\nawait session.post(url, data=data, json=json)","typeGuard":"def has_unique_body(data, json_) -> bool:\n    return not (data is not None and json_ is not None)","tryCatchPattern":"try:\n    resp = await session.post(url, data=data, json=json_)\nexcept ValueError as e:\n    if 'data and json' in str(e):\n        # pick one body form\n        resp = await session.post(url, json=json_)\n    else:\n        raise","preventionTips":["In wrapper functions, accept either data or json explicitly, not both via **kwargs.","Add a pre-call assertion that exactly one body argument is set.","Document request helpers with the chosen body convention."],"tags":["http-request","request-body","validation"],"backgroundTag":null,"analyzedSha":"d041d4d0fd48c3f0832084d33be16cf1c4835f85","analyzedAt":"2026-08-11T20:44:15.550Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}