python data_error ai_generated true

aiohttp.client_exceptions.ContentTypeError: 0, message='Attempt to decode JSON with unexpected mimetype: text/html'

ID: python/aiohttp-content-type-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2025-05-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.7 active
3.8 active
3.9 active

Root Cause

Calling resp.json() on a response with a non-JSON content type.

generic

中文

在具有非JSON内容类型的响应上调用resp.json()。

Workarounds

  1. 95% success
    if resp.content_type == 'application/json': data = await resp.json() else: text = await resp.text()
  2. 90% success
    data = await resp.json(content_type=None)

Dead Ends

Common approaches that don't work:

  1. 70% fail

    It's a client-side parameter, not a server-side fix.

  2. 20% fail

    Works but bypasses aiohttp's built-in error handling.