Flask 中请求钩子的理解和应用?

请求钩子是通过装饰器的形式实现的,支持以下四种:
1,before_first_request 在处理第一个请求前运行
2,before_request:在每次请求前运行
3,after_request:如果没有未处理的异常抛出,在每次请求后运行
4,teardown_request:即使有未处理的异常抛出,在每次请求后运行

# 请求钩子

1. @api.after_request
2. def after_request(response):
3. """设置默认的响应报文格式为 application/json"""
4. # 如果响应报文 response 的 Content-Type 是以 text 开头,则将其改为
5. # 默认的 json 类型
6. if response.headers.get("Content-Type").startswith("text"):
7. response.headers["Content-Type"] = "application/json"
8. return respon
原文地址:https://www.cnblogs.com/lmh001/p/9742230.html