gpt4 book ai didi

python - Flask 发布 JSON

转载 作者:行者123 更新时间:2023-11-30 22:37:30 24 4
gpt4 key购买 nike

我有以下 flask 路线:

@app.route('/change_groups', methods = ['POST'])
def change_groups():
if not request.json:
return "Not a json post"
return "json post!"

当我像这样 curl 时,我会回来"Not a json post"正如预期的那样:

curl --data "param1=value1&param2=value2" localhost:8000/change_groups

然后我尝试发布 json 来触发 "json post!"代码:

curl -X POST -d '{"username":"xyz","password":"xyz"}' http://localhost:8000/change_groups

第二个请求也会触发"Not a json post"当我期待"json post!" .

如何将 json 发送到此 Flask 路由?

最佳答案

在您的请求中,您没有设置Content-Type: application/json,因此它不会被解析为 JSON。

关于python - Flask 发布 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43874623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com