diff --git a/src/keycloak/connection.py b/src/keycloak/connection.py index 400ee23..19317e0 100644 --- a/src/keycloak/connection.py +++ b/src/keycloak/connection.py @@ -395,7 +395,7 @@ class ConnectionManager: method="POST", url=urljoin(self.base_url, path), params=self._filter_query_params(kwargs), - data=data, + **self._prepare_httpx_request_content(data), headers=self.headers, timeout=self.timeout, ) @@ -421,7 +421,7 @@ class ConnectionManager: return await self.async_s.put( urljoin(self.base_url, path), params=self._filter_query_params(kwargs), - data=data, + **self._prepare_httpx_request_content(data), headers=self.headers, timeout=self.timeout, ) @@ -452,7 +452,7 @@ class ConnectionManager: return await self.async_s.request( method="DELETE", url=urljoin(self.base_url, path), - data=data or {}, + **self._prepare_httpx_request_content(data or {}), params=self._filter_query_params(kwargs), headers=self.headers, timeout=self.timeout, @@ -461,6 +461,23 @@ class ConnectionManager: msg = "Can't connect to server" raise KeycloakConnectionError(msg) from e + @staticmethod + def _prepare_httpx_request_content(data: dict | str | None) -> dict: + """ + Create the correct request content kwarg to `httpx.AsyncClient.request()`. + + See https://www.python-httpx.org/compatibility/#request-content + + :param data: the request content + :type data: dict | str | None + :returns: A dict mapping the correct kwarg to the request content + :rtype: dict + """ + if isinstance(data, str): + # Note: this could also accept bytes, Iterable[bytes], or AsyncIterable[bytes] + return {"content": data} + return {"data": data} + @staticmethod def _filter_query_params(query_params: dict) -> dict: """ diff --git a/tests/test_keycloak_admin.py b/tests/test_keycloak_admin.py index 5f52f72..a90b6d5 100644 --- a/tests/test_keycloak_admin.py +++ b/tests/test_keycloak_admin.py @@ -6156,7 +6156,7 @@ async def test_a_email_query_param_handling(admin: KeycloakAdmin, user: str) -> mock_put.assert_awaited_once_with( ANY, - data='["UPDATE_PASSWORD"]', + content='["UPDATE_PASSWORD"]', params={"client_id": "update-account-client-id", "redirect_uri": "https://example.com"}, headers=ANY, timeout=60,