Python 无法读取Flask中已发布的应用程序/json数据
我有一个flask应用程序,它有一个端点:Python 无法读取Flask中已发布的应用程序/json数据,python,flask,Python,Flask,我有一个flask应用程序,它有一个端点: @app.route('/restaurant/<restaurant_id>', methods=['GET', 'POST']) def detail(restaurant_id=None): if request.method == 'GET': #something elif request.method == 'POST': print jsonify(request.get_jso
@app.route('/restaurant/<restaurant_id>', methods=['GET', 'POST'])
def detail(restaurant_id=None):
if request.method == 'GET':
#something
elif request.method == 'POST':
print jsonify(request.get_json(force=True)) #This doesn't work if I set content type as applicaiton/json
comments = [request.form['comment']] #this works if I set content type as form-data
# something
return Response(json.dumps({
'success': True,
'response': json_docs
}), status=200, content_type='application/json')
@app.route('/restaurant/',methods=['GET','POST'])
def详细信息(餐厅id=无):
如果request.method==“GET”:
#某物
elif request.method==“POST”:
print jsonify(request.get_json(force=True))#如果我将内容类型设置为applicationon/json,这将不起作用
comments=[request.form['comment']]#如果我将内容类型设置为表单数据,则此操作有效
#某物
返回响应(json.dumps)({
"成功":对,,
“响应”:json_文档
}),status=200,content_type='application/json')
由于某些原因,当发布的数据作为JSON发布时,我无法读取它,而
应用程序/JSON
作为内容类型
。print命令只返回None
。如何读取Json数据?请求。get_Json()
返回Json,因此jsonify
应将其转换回Json。注释应该是request.get_json(force=True)
。希望这能有所帮助。是否要获取json数据get_json()
应该足够了,为什么要使用jsonify()
,它通常用于创建带有json表示的响应。