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

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-04-16First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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

  1. 93% success
    data = request.get_json(silent=True) or {}
    if not data:
        return jsonify(error='invalid json'), 400
  2. 95% success
    requests.post(url, json={'k': 'v'})  # sets application/json automatically

Dead Ends

Common approaches that don't work:

  1. 95% fail

    Output formatting config, not input parsing.

  2. 80% fail

    Silently accepts malformed input, masks client bugs.