How to get POSTed JSON in Flask?

前端 未结 10 1129
隐瞒了意图╮
隐瞒了意图╮ 2020-11-21 06:35

I\'m trying to build a simple API using Flask, in which I now want to read some POSTed JSON. I do the POST with the Postman Chrome extension, and the JSON I POST is simply <

10条回答
  •  野的像风
    2020-11-21 06:51

    To give another approach.

    from flask import Flask, jsonify, request
    app = Flask(__name__)
    
    @app.route('/service', methods=['POST'])
    def service():
        data = json.loads(request.data)
        text = data.get("text",None)
        if text is None:
            return jsonify({"message":"text not found"})
        else:
            return jsonify(data)
    
    if __name__ == '__main__':
        app.run(host= '0.0.0.0',debug=True)
    

提交回复
热议问题