• 技术文章 >Python框架 >Flask

    flask如何接收json

    silencementsilencement2019-07-27 10:47:50原创6940

    下面来说一下Flask中接收与发送json方式

    1、利用flask的request.form.get()方法接收

    from flask import Flask
    from flask import jsonify
    from flask import request
    import json
     
    @app.route("/login", methods=['POST'])
    def login():
    	 data_ = request.form.get('data')
    	 data = json.loads(data)
    	 username = data['username']
    	 password = data['password']
    
    	 return jsonify({"login": Login.login(username, password)})  # 返回布尔值

    2、 利用flask的request.get_data()方法接收

    from flask import Flask
    from flask import jsonify
    from flask import request
    import json
    
    @app.route("/flask/login", methods=['POST'])
    def login():
     data = request.get_data()
     data = json.loads(data)
     username = data['username']
     password = data['password']
    
     return jsonify({"login": Login.login(username, password)}) # 返回布尔值

    3、利用flask的request.get_json()方法接收

    from flask import Flask
    from flask import jsonify
    from flask import request
    
    
    @app.route("/flask/login", methods=['POST'])
    def login():
     data = request.get_json()
     username = data['username']
     password = data['password']
     
     return jsonify({"login": Login.login(username, password)}) # 返回布尔值
    专题推荐:json
    上一篇:flask多选框怎么实现 下一篇:flask多线程模式

    相关文章推荐

    • Python搭建的第一个flask应用实例• django和flask哪个好• flask多选框怎么实现

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网