前言
一般在接口开发中,会定制统一的返回JSON 格式如:{‘code’: 0, ‘message’: ‘success’, ‘data’: {}}
定制JSON 格式
Flask-RESTX 的Api对象提供了一个representation的装饰器,允许定制返回数据的呈现格式
基本示例
api = Api(app)
@api.representation('application/json')
def output_json(data, code, headers):
# TODO ..... 添加自定义处理
return resp
在flask_restful.representations.py
文件源码中有一个output_json 方法
def output_json(data, code, headers=None):
"""Makes a Flask response with a JSON encoded body"""
settings = current_app.config.get("RESTX_JSON", {})
# If we're in debug mode, and the indent is not set, we set it to a
# reasona