gpt4 book ai didi

python - Flask RESTful POST JSON 失败

转载 作者:太空狗 更新时间:2023-10-30 01:01:49 24 4
gpt4 key购买 nike

我在通过 curl 从 cmd (Windows7) 向 Flask RESTful 发布 JSON 时遇到问题。这是我发布的内容:

curl.exe -i -H "Content-Type: application/json" \
-H "Accept: application/json" -X POST \
-d '{"Hello":"Karl"}' http://example.net:5000/

它会导致一个错误的请求,我也不知道如何调试它,通常我会打印出信息到控制台,但这不起作用。你如何调试 wsgi 应用程序?似乎是一项无望的任务......

这是我在网上看到的简单测试应用程序:

from flask import Flask, request
from flask.ext.restful import Resource, Api

app = Flask(__name__)
api = Api(app)

class Test(Resource):
def post(self):
#printing request.data works
json_data = request.get_json(force=True) # this issues Bad request
# request.json also does not work
return {}

api.add_resource(Test, '/')

if __name__ == '__main__':
app.run(debug=True)

最佳答案

-d '{"Hello":"Karl"}' 在 Windows 中不起作用,因为它被单引号括起来。使用双引号,它会为你工作。

-d "{\"Hello\":\"Karl\"}"

关于python - Flask RESTful POST JSON 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22273671/

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