AsyncTwilioHttpClient does not JSON-serialize request bodies for application/json endpoints (sync client does) → 400 on JSON-body v2 endpoints
Maintainer antworten meist innerhalb von 1 Tag
Bewertung
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Anfängerfreundlichkeit
- 82/100
Rechercherichtung
Beginne in twilio/http/async_http_client.py bei AsyncTwilioHttpClient.request und vergleiche anschließend die Erstellung der Anfrage mit twilio/http/http_client.py. Überprüfe, dass der asynchrone Client Bodies vom Typ application/json und application/scim+json wie der synchrone Client verarbeitet, während form-encoded Endpoints weiterhin unbeeinträchtigt bleiben; reproduziere dies mit dem update_async-Beispiel für Channels Senders und bestätige, dass der Request-Body gültiges JSON ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Environment
twilio(twilio-python): 9.10.9- Python: 3.11.15
aiohttp: 3.13.5
Description
The async HTTP client (AsyncTwilioHttpClient) is missing the Content-Type: application/json branch that the sync client has, so for any endpoint that sends a JSON request body it passes the body dict to aiohttp's data= (which form-encodes it) while still sending Content-Type: application/json. Twilio receives a body that isn't valid JSON and rejects it with 400 "Request input may be invalid".
This breaks every newer v2 JSON-body endpoint on the async client. Concrete case: client.messaging.v2.channels_senders(sid).update_async(...) (update a WhatsApp sender's business profile). The identical call on the sync client succeeds.
Root cause
Sync client serializes JSON correctly — twilio/http/http_client.py:
if headers and headers.get("Content-Type") == "application/json":
kwargs["json"] = data # real JSON body
elif headers and headers.get("Content-Type") == "application/scim+json":
kwargs["json"] = data
else:
kwargs["data"] = data
Async client has no such branch — twilio/http/async_http_client.py (request):
kwargs = {
"method": method.upper(),
"url": url,
"params": params,
"data": data, # always form-encoded, even for application/json
"headers": headers,
...
}
...
response = await session.request(**kwargs)
The generated code (e.g. messaging/v2/channels_sender.py::_update_async) correctly sets headers["Content-Type"] = "application/json" and passes data = <dict>, relying on the transport to JSON-encode — which the async client never does.
Reproduction
import asyncio
from twilio.rest import Client
from twilio.http.async_http_client import AsyncTwilioHttpClient
from twilio.rest.messaging.v2.channels_sender import ChannelsSenderList as CSL
async def main():
client = Client(SID, TOKEN, http_client=AsyncTwilioHttpClient())
body = CSL.MessagingV2ChannelsSenderRequestsUpdate(
{"profile": CSL.MessagingV2ChannelsSenderProfile({"logo_url": "https://example.com/x.jpg"})}
)
await client.messaging.v2.channels_senders("XE...").update_async(body) # -> 400
asyncio.run(main())
Result: TwilioRestException(400, '/Channels/Senders/XE...', 'Unable to update record: Request input may be invalid...').
Inspecting the captured request (http_client._test_only_last_request): data is a dict ({'profile': {'logo_url': '...'}}), not a JSON string, with Content-Type: application/json. The same update(body) on the sync TwilioHttpClient returns 200.
Expected
The async client should mirror the sync client: when Content-Type is application/json (or application/scim+json), send the body via aiohttp's json= rather than data=.
Suggested fix
In AsyncTwilioHttpClient.request, replace the unconditional "data": data with the same content-type branch the sync client uses:
if headers and headers.get("Content-Type") in ("application/json", "application/scim+json"):
kwargs["json"] = data
else:
kwargs["data"] = data
Impact
Any async call to a JSON-body endpoint fails (Channels Senders create/update, and other v2 resources that send a JSON schema body). Form-encoded endpoints (Messages, Calls, Lookups, etc.) are unaffected. Current workaround: send the request directly (e.g. httpx/aiohttp with json=), which is what the sync client does internally.
- Vorherrschende Sprache
- Python
- Sterne
- 2.1k
- Forks
- 821
- Ø Merge
- 9 Std. 33 Min.
- Gemergte PRs (30 T.)
- 3
Entwicklungsumgebung
- Enthält ein Dockerfile oder eine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus twilio/twilio-python
-
Docs: "Handling Exceptions" should mention that network errors are not `TwilioRestException`Evtl. vergeben @henricop hat das vor 13 Tagen übernommen. Offen
Schwierigkeit 1/5 1-3 Stunden Anfängerfreundlichkeit 90/100
twilio/twilio-python#986 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Clearer diagnostic when an API Key SID is passed without `account_sid`Evtl. vergeben @saryeljaramany hat das vor 5 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
twilio/twilio-python#985 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
TokenPagination.next_page_async() makes a sync request and fails with an async http clientEvtl. vergeben @sebxt hat das vor 31 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
twilio/twilio-python#976 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 88/100
twilio/twilio-python#930 ·
Maintainer antworten meist innerhalb von 1 Tag
-
[BUG] AsyncTwilioHttpClient.request does not use self.timeoutEvtl. vergeben @Hashim1999164 hat das vor 96 Tagen übernommen. Offentype: bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
twilio/twilio-python#925 ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in twilio/twilio-python
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 64/100
Maintainer antworten meist innerhalb von 1 Tag
-
Harden tools/pr_threads.sh: pass all printed thread fields through the deterministic sanitizerOffenarea:cli area:security enhancement priority:medium size:small
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
RonaldHensbergen/composable-data-stack#1338 ·
Maintainer antworten meist innerhalb von 1 Tag
-
area:stdlib effort:S impact:med platform:linux type:bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 84/100
Maintainer antworten meist innerhalb von 1 Tag
-
automated endpoint-dead needs-curation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
almanac-data/climate-almanac#139 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
UKGovernmentBEIS/inspect_ai#6695 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag