工作的时候经常写api,记一下规范
1. GET请求 接收参数用args,尽量不用json作为请求体
@controller.route('/query_log', methods=["GET"])
def query_log():para1 = request.args.get('para1')para2 = request.args.get('para2')# 处理过程response_data = {"code": HTTPStatus.OK.value,"message": "OK","data": data_list}return make_response(jsonify(response_data), HTTPStatus.OK.value)
调接口直接在url后边接参数
http://127.0.0.1/query_log?para1=a¶2=b
2 POST 请求 使用get_json()接收参数
@controller.route('/add_log', methods=["POST"])
def query_log():data = request.get_json()para1 = data['para1']para2 = data['para2']# 处理过程response_data = {"code": HTTPStatus.OK.value,"message": "OK","data": data_list}return make_response(jsonify(response_data), HTTPStatus.OK.value)
调接口需要使用json传参数
{"para1": 111,"para2": 222
}