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

其他格式: JSON · Markdown 中文 · English
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.

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 95% 失败

    Output formatting config, not input parsing.

  2. 80% 失败

    Silently accepts malformed input, masks client bugs.