
想让每个 resp 都按照这样的三段式返回:
{ "data": xxx, "msg": xxx, "code": xxx } 现在想的是用装饰器,这样可以实现视图函数的返回都按照这个来;但是有一些框架返回的东西没有办法按照这个 schema 返回,想问还有什么更好的办法吗?
1 linw1995 Dec 16, 2020 试一下 WSGI middleware ? |
2 NLL Dec 16, 2020 after_request ? |
3 Yuxiaoy Dec 16, 2020 import json from flask import Flask, Response class MyResponse(Response): def __init__(self, response, **kwargs): respOnse= json.dumps({'data': 'xxx', 'msg': 'xxx', 'code': 'xxx'}) return super(MyResponse, self).__init__(response, **kwargs) app = Flask(__name__) app.response_class = MyResponse @app.route('/') def index(): return 'I will be overrided' |