【问题标题】:Flask 400 Bad Request The browser (or proxy) sent a request that this server could not understand JSONFlask 400 Bad Request 浏览器(或代理)发送了此服务器无法理解 JSON 的请求
【发布时间】:2020-06-11 07:53:23
【问题描述】:

我正在尝试通过 curl 将以下 JSON 对象发送到我的 Flask 应用程序,但出现错误 400。我认为这是因为脚本中的值具有许多特殊字符,当我将那里的值替换为“测试”时,它工作正常,我正在使用 Windows。

curl 请求

curl -X POST -d @example_post.json http://test/hlt  --header "Content-Type:application/json; charset=utf-8"

数据:

{
    "item-type": "Magazine",
    "clip-id": "",
    "source-language": null,
    "target-language": "de",
    "item-id": "0000000b000ca0d5",
    "item-title": "Drone Malaui | SHIS200207_001_MalawiDrones_01F",
    "publication-title": "",
    "video-path": "test_files/TestFile.mp4",
    "ftp-url": "test_files/TestFile.mp4",
    "script": " ++++++++++++++++++++++++++++++++++++++++++++++++++Bilder:  The_power_of_hope1.KONF.20728229++++++++++++++++++++++++++++++++++++++++++++++++++Première:  SHIS200207_001_MalawiDrones_01F++++++++++++++++++++++++++++++++++++++++++++++++++Titel:evo aquí he hecho este drone. Lo construí con mis propias manos, con los materiales que nos proporcionan en el curso. Nos dieron los materiales y nos explicaron cómo emplearlos. Seguí las instrucciones y aquí está el resultado.\\\"+++1:34Ellos son los 26 primeros alumnos de los 150 que la academia planea formar antes de finales de 2021. A partir de 2022, está previsto ofrecer una maestría en tecnología de drones. 1:49  +++SOT Rudolf Schwenk, UNICEF Malawi country representative+++\\\"Es un paso importante para ayudar a los jóvenes a adquirir habilidades del siglo XXI. La década que recién comenzó va a ser digital, y ayudar a los jóvenes a mejorar sus habilidades y aprender tecnología digital y manejo de drones va a suponer un avance enorme, no sólo para Malawi, sino también para muchos otros países de África.\\\"+++2:13Gracias a jóvenes como ellos, la tecnología de los drones con fines humanitarios podrá levantar vuelo y servir de ejemplo a otros países. "
}

app.py

@app.route('/mydomain', methods=['POST'])

    def upload_videos():
        print('request=', request.json, file=sys.stderr)
        if request.method == "POST":
            data = request.get_json()
            if data is not None:
               # print('request=', request, file=sys.stderr)
                res = upload_file(data, bucket)
                print(res, file=sys.stderr)
                return(res)
        return 'no data found'

【问题讨论】:

  • 在您的情况下res 的类型是什么?你可以尝试将它包装成 jsonify 吗?
  • 我尝试在 Docker 容器中运行您的代码,它运行良好。备注:我无权访问upload_file函数,所以我返回输入json。
  • 使用包含非英文字符的json值产生错误。并且只发生在 Windows 系统上,但是当我将文件保存为 utf-8 时,它会通过
  • 但是,当我只将 --header "Content-Type:application/json; charset=utf-8" 添加到我的 curl 请求中但不将文件另存为 utf-8 时,它不会通过
  • 看看这个blog.furas.pl/python-unicode-decode-encode.html我认为如果你使用latin2或windows-1250会更好。

标签: python json curl flask uwsgi


【解决方案1】:

您是否尝试将字符集设置为“latin-1”(ISO-8859-1)?这通常是处理 Windows 数据时的问题。拉丁语 1 表示 Unicode 字符集的前 256 个代码点。代码点 0 - 127 被编码为就像使用 UTF-8 一样,但代码点 128 - 255 被编码为使用 Latin-1 的单字节和使用 UTF-8 的 2 字节序列。请参阅https://en.wikipedia.org/wiki/ISO/IEC_8859-1 了解更多信息。

【讨论】:

    猜你喜欢
    • 2021-02-19
    • 2021-12-04
    • 2020-10-17
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 2018-12-20
    • 2018-06-21
    相关资源
    最近更新 更多