{"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/c0ef574e29109210e96e652771ae4e7b88615fa4/aiohttp/client.py#L498-L534","documentation":"Raised in `_request` (client.py:515-518) when both `data` and `json` are non-None on the same call. aiohttp needs to choose one body encoding strategy — `json=` wraps the value in `JsonPayload` (or `JsonBytesPayload`), while `data=` is for form/stream/raw bodies — and the two are mutually exclusive. The guard short-circuits before any payload is built.","triggerScenarios":"Calling `session.post(url, data=some_dict, json=some_other_dict)`, or forwarding a request helper that always sets both kwargs (e.g., `**kwargs` merged with a default `json=`).","commonSituations":"Refactoring a helper that previously took `data=` to also support `json=` and forgetting to make them exclusive; merging per-call overrides onto default kwargs via dict update that leaves both populated; OpenAPI-generated clients populating both fields.","solutions":["Pick one: pass the payload as `json=payload` for JSON, or `data=payload` for form/raw.","In forwarding helpers, explicitly pop the unused one: `kwargs.pop('data', None) if 'json' in kwargs else None`.","Use `aiohttp.payload.JsonPayload(obj)` explicitly if you need to send it via `data=` for unusual cases."],"exampleFix":"// before\nawait session.post(url, data=form, json=payload)\n// after\nawait session.post(url, json=payload)   # JSON body\n// or\nawait session.post(url, data=form)      # form / raw body","handlingStrategy":"validation","validationCode":"def build_kwargs(kwargs):\n    if kwargs.get('data') is not None and kwargs.get('json') is not None:\n        raise ValueError('pass either data= or json=, not both')\n    return kwargs","typeGuard":null,"tryCatchPattern":"try:\n    await session.post(url, **kwargs)\nexcept ValueError as e:\n    if 'data and json' in str(e):\n        kwargs.pop('data', None)  # keep json\n        await session.post(url, **kwargs)\n    else:\n        raise","preventionTips":["Make forwarding helpers choose one body kwarg explicitly.","In wrapper functions, pop the unused alternative before delegating.","Add a unit test that asserts your helper rejects both-set calls."],"tags":["client","request-body","api-misuse","validation"],"analyzedSha":"c0ef574e29109210e96e652771ae4e7b88615fa4","analyzedAt":"2026-08-04T19:51:05.467Z","schemaVersion":2}