From 4ed73438f00eac2b47c288eda1be658bd7bbd202 Mon Sep 17 00:00:00 2001 From: Derek Allan Boman Date: Mon, 27 Jul 2026 18:48:21 -0700 Subject: [PATCH] Fix JSON serialization in async HTTP client --- tests/unit/http/test_async_http_client.py | 30 +++++++++++++++++++++++ twilio/http/async_http_client.py | 8 +++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/tests/unit/http/test_async_http_client.py b/tests/unit/http/test_async_http_client.py index 23df35dc02..05c71fa57f 100644 --- a/tests/unit/http/test_async_http_client.py +++ b/tests/unit/http/test_async_http_client.py @@ -43,6 +43,36 @@ async def test_request_called_with_method_and_url(self): self.assertEqual(request_args["method"], "GET") self.assertEqual(request_args["url"], "https://mock.twilio.com") + async def test_request_body_is_passed_as_json_for_application_json(self): + body = {"userName": "Alice"} + + await self.client.request( + "POST", + "https://mock.twilio.com", + data=body, + headers={"Content-Type": "application/json"}, + ) + + request_args = self.session_mock.request.call_args.kwargs + + self.assertEqual(request_args["json"], body) + self.assertNotIn("data", request_args) + + async def test_request_body_is_passed_as_json_for_scim_json(self): + body = {"userName": "Alice"} + + await self.client.request( + "POST", + "https://mock.twilio.com", + data=body, + headers={"Content-Type": "application/scim+json"}, + ) + + request_args = self.session_mock.request.call_args.kwargs + + self.assertEqual(request_args["json"], body) + self.assertNotIn("data", request_args) + async def test_request_called_with_basic_auth(self): await self.client.request( "doesnt matter", "doesnt matter", auth=("account_sid", "auth_token") diff --git a/twilio/http/async_http_client.py b/twilio/http/async_http_client.py index ecd5d4de95..2a0f1c00f6 100644 --- a/twilio/http/async_http_client.py +++ b/twilio/http/async_http_client.py @@ -87,13 +87,19 @@ async def request( "method": method.upper(), "url": url, "params": params, - "data": data, "headers": headers, "auth": basic_auth, "timeout": timeout, "allow_redirects": allow_redirects, } + content_type = (headers or {}).get("Content-Type", "").lower() + + if content_type in ("application/json", "application/scim+json"): + kwargs["json"] = data + else: + kwargs["data"] = data + self.log_request(kwargs) self._test_only_last_response = None