python_flask框架学习笔记二[不同请求方法]
flask不同请求方法的应用:
1、GET
@app.route('/meter/test', methods=['GET']) def index01(): name = request.args.get("name") content = { 'resultCode': 0, 'message': "操作成功", 'total': 290 } if name == 'vivage': return jsonify(**content) else: return u'传参错误'
2、POST
@app.route('/meter/testpost', methods=['POST']) def index02(): username = request.json.get("username") password = request.json.get("password") content = { 'resultCode': 0, 'message': "登录成功" } if username == 'test' and password == '0': return jsonify(**content) else: return u'账号/密码不正确'
3、DELETE
@app.route('/meter/testdelete/', methods=['DELETE']) def index03(): id = request.json.get("id") content1 = { 'resultCode': 0, 'message': "删除成功" } content2 = { "resultCode": 10002, "message": "该部门不存在" } if id == 123: return jsonify(**content1) else: return jsonify(**content2) @app.route('/meter/testdelete/<int:id>', methods=['DELETE']) def index04(id): content1 = { 'resultCode': 0, 'message': "删除成功" } content2 = { "resultCode": 10002, "message": "该部门不存在" } if id == 123: return jsonify(**content1) else: return jsonify(**content2)
4、PUT
@app.route('/meter/testput/', methods=['PUT']) def index05(): name = request.json.get("name") age = request.json.get("age") if name == 'vivage': age = age -1 return jsonify(name=name, age=age) else: return jsonify(msg="用户不存在,无法修改")