pip install pre_request
from pre_request import pre, Rule
fields = {
"param1": Rule(type=str, required=True, dest="param1"),
"param2": Rule(type=list, required=True, dest="param2")
}
@app.route("/app/query", methods=["post", "get"])
@pre.catch(fields)
def query():
param1 = param("param1", request)
param2 = param_list("param2", request)
def param(field, request):
if request.method == "GET":
return request.args.get(field)
elif request.method == "POST":
return request.json.get(field)
raise Exception("不支持这种请求方法")
def param_list(field, request):
if request.method == "GET":
return request.args.getlist(field)
elif request.method == "POST":
return request.json.get(field)
raise Exception("不支持这种请求方法")