python – 通过Curl向Flask发送JSON-Request [复制]
发布时间:2020-12-16 23:33:30 所属栏目:Python 来源:网络整理
导读:参见英文答案 How to get POSTed json in Flask?4个 How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?17个 我在烧瓶中设置了一个非常简单的邮政路线: from flask import Flask,requestapp = Flask(__name__)@app.route('/p
参见英文答案 >
How to get POSTed json in Flask?4个
> How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?17个 我在烧瓶中设置了一个非常简单的邮政路线: from flask import Flask,request app = Flask(__name__) @app.route('/post',methods=['POST']) def post_route(): if request.method == 'POST': data = request.get_json() print('Data Received: "{data}"'.format(data=data)) return "Request Processed.n" app.run() 这是我尝试从命令行发送的curl请求: curl localhost:5000/post -d '{"foo": "bar"}' 但仍然打印出“收到的数据:”无“”.所以,它无法识别我传递的JSON. 在这种情况下是否有必要指定json格式? 解决方法
根据
get_json 文档:
因此,要么将传入请求的mimetype指定为application / json: curl localhost:5000/post -d '{"foo": "bar"}' -H 'Content-Type: application/json' 或使用force = True强制进行JSON解码: data = request.get_json(force=True) 如果在Windows上运行此命令(cmd.exe,而不是PowerShell),则还需要更改JSON数据的引用,从单引号到双引号: curl localhost:5000/post -d "{"foo": "bar"}" -H 'Content-Type: application/json' (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |