无法在Flask中读取POSTed application / json数据

时间:2015-12-08 08:02:36

标签: python 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_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')

出于某种原因,我将无法在将application/json作为Content-Type作为JSON发布时读取该数据。 print命令只返回None。我如何阅读Json数据?

1 个答案:

答案 0 :(得分:0)

request.get_json()返回JSON,因此jsonify应该将其转回JSON。评论应为request.get_json(force=True)。希望这会有所帮助。