python data_error ai_generated true

aiohttp客户端异常:尝试用意外MIME类型解码JSON: text/html

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

ID: python/aiohttp-content-type-error

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2025-05-01首次发现

版本兼容性

版本状态引入弃用备注
3.7 active
3.8 active
3.9 active

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 70% 失败

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

  2. 20% 失败

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