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
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).
解决方案
-
95% 成功率
curl -X POST -H 'Content-Type: application/json' -d '{"a":1}' http://localhost:5000/api -
88% 成功率
data = request.get_json(silent=True) or {} # explicit fallback for non-JSON
无效尝试
常见但无效的做法:
-
55% 失败
Hides the contract bug and can produce confusing 400s on malformed bodies.
-
70% 失败
Returns wrong status; client still gets no data.