• 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来转换最后的结果

  • 相关阅读:
    初入水:vector
    Sort Colors
    Palindrome Partitioning II
    Search for a Range
    Container With Most Water
    Palindrome Partitioning
    Longest Consecutive Sequence
    简单写了一个堆排序
    Best Time to Buy and Sell Stock III
    4-7
  • 原文地址:https://www.cnblogs.com/daryl-blog/p/11002990.html
Copyright © 2020-2023  润新知