python
data_error
ai_generated
true
werkzeug.exceptions.BadRequest: 400 错误请求:浏览器(或代理)发送了服务器无法理解的请求。
werkzeug.exceptions.BadRequest: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
ID: python/flask-json-decode-bad-request
80%修复率
88%置信度
0证据数
2024-04-16首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 2.x | active | — | — | — |
| 3.x | active | — | — | — |
根因分析
在缺少 `Content-Type: application/json` 或 JSON 格式错误的请求上调用 `request.get_json()`。
English
`request.get_json()` was called on a request without `Content-Type: application/json` or with malformed JSON.
解决方案
-
93% 成功率
data = request.get_json(silent=True) or {} if not data: return jsonify(error='invalid json'), 400 -
95% 成功率
requests.post(url, json={'k': 'v'}) # sets application/json automatically
无效尝试
常见但无效的做法:
-
95% 失败
Output formatting config, not input parsing.
-
80% 失败
Silently accepts malformed input, masks client bugs.