flask返回自定义的Response


from json import dumps
from flask import Response
from flask_api import status
from protocol.errors_pb2 import *


class ErrorResponse(Response):
    def __init__(self, err_code, err_msg=''):
        result = dumps(dict(code=err_code, msg=err_msg))
        Response.__init__(self, result, mimetype='application/json')


class JSONResponse(Response):
    def __init__(self, data, msg=''):
        result = dumps(dict(data=data, code=Error_None, msg=msg))
        Response.__init__(self, result, mimetype='application/json')


class UnauthorizedResponse(Response):
    def __init__(self):
        data = dumps(dict(msg="need login", code=Error_NeedLogin, data=None))
        Response.__init__(self, data, mimetype='application/json', status=status.HTTP_401_UNAUTHORIZED)

注意:一定要使用json.dumps来转换最后的结果


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM