【问题标题】:Carry extra info in error reponse在错误响应中携带额外信息
【发布时间】:2012-02-29 09:48:47
【问题描述】:

在错误类型响应中返回额外数据(例如错误代码)的最佳方式是什么。 #Method 1 不携带额外信息,而#Method 2 不起作用。

#method 1
ImmediateHttpResponse(HttpBadRequest('error')) #cant carry extra info


#method 2
class ErrorInfo(object):
    msg_id = ''
    msg_code = ''
    msg_content = ''

class Resource(Resource):
    ....
    def get_object_list(self, request):
        if ...:
            error_info = ErrorInfo()
            error_info.msg_id = 'rt'
            error_info.msg_code = 'rerer'
            obj  = {'objects': error_info}
            return self.create_response(request, obj)

【问题讨论】:

    标签: python django tastypie


    【解决方案1】:

    如果有帮助,您可以使用 ImmediateHttpResponse(HttpResquest([...], status=403))

    【讨论】:

    • 是的,有效。 raise ImmediateHttpResponse(HttpBadRequest([{'msg_id':'1','msg_code':'r4r'}], status=403))
    • status=403 用于“禁止”。对于错误请求,使用 status=400。当您使用 HttpBadRequest 时,默认为 400。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 2015-08-04
    • 2017-04-08
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多