AsyncTwilioHttpClient does not JSON-serialize request bodies for application/json endpoints (sync client does) → 400 on JSON-body v2 endpoints

Offen Anfängerfreundlich
#932 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

@Steve0x2a arbeitet bereits daran.

Seit 04.9.2026.

  • #964 von @Steve0x2a — offen

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
82/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Ruhig
Tech-Stack
python
Bereich
api

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

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus twilio/twilio-python

Alle Issues in twilio/twilio-python

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.