python
data_error
ai_generated
true
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%Fix Rate
88%Confidence
0Evidence
2024-04-16First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 2.x | active | — | — | — |
| 3.x | active | — | — | — |
Root Cause
`request.get_json()` was called on a request without `Content-Type: application/json` or with malformed JSON.
generic中文
在缺少 `Content-Type: application/json` 或 JSON 格式错误的请求上调用 `request.get_json()`。
Workarounds
-
93% success
data = request.get_json(silent=True) or {} if not data: return jsonify(error='invalid json'), 400 -
95% success
requests.post(url, json={'k': 'v'}) # sets application/json automatically
Dead Ends
Common approaches that don't work:
-
95% fail
Output formatting config, not input parsing.
-
80% fail
Silently accepts malformed input, masks client bugs.