python data_error ai_generated true

werkzeug.exceptions.UnsupportedMediaType: 415 不支持的媒体类型:请求 Content-Type 不是 'application/json',未尝试加载 JSON 数据。

werkzeug.exceptions.UnsupportedMediaType: 415 Unsupported Media Type: Did not attempt to load JSON data because the request Content-Type was not 'application/json'.

ID: python/flask-json-415-unsupported-media-type

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

版本兼容性

版本状态引入弃用备注
2.x active — — —
3.x active — — —

根因分析

调用了 request.get_json(),但客户端未发送 Content-Type: application/json 头(常见于 curl 或 fetch 默认行为)。

English

request.get_json() called but the client sent no Content-Type: application/json header (common with curl or fetch defaults).

generic

解决方案

  1. 95% 成功率
    curl -X POST -H 'Content-Type: application/json' -d '{"a":1}' http://localhost:5000/api
  2. 88% 成功率
    data = request.get_json(silent=True) or {}  # explicit fallback for non-JSON

无效尝试

常见但无效的做法:

  1. 55% 失败

    Hides the contract bug and can produce confusing 400s on malformed bodies.

  2. 70% 失败

    Returns wrong status; client still gets no data.