简体   繁体   English

向 APIException 添加自定义响应标头

[英]Adding custom response Headers to APIException

I have created a custom exception referring to http://django-rest-framework.org/api-guide/exceptions.html .我创建了一个参考http://django-rest-framework.org/api-guide/exceptions.html的自定义异常。

Please know that I have my own authentication backend.请知道我有自己的身份验证后端。 Hence I am not using rest_framework's authentication module.因此我没有使用 rest_framework 的身份验证模块。

For authentication errors, I want to add 'WWW-Authenticate: Token' header to the response that is sent from the exception.对于身份验证错误,我想将“WWW-Authenticate: Token”标头添加到从异常发送的响应中。

Any ideas will be very helpful.任何想法都会非常有帮助。

Update:更新:

Thanks @Pathétique, This is what I ended up doing.谢谢@Pathétique,这就是我最终要做的。

-Have a base view class named BaseView. - 有一个名为 BaseView 的基本视图类。

-override the handle_exception method to set appropriate headers, in my case 'WWW-Authenticate'. - 覆盖 handle_exception 方法以设置适当的标头,在我的例子中是“WWW-Authenticate”。

Here is the code:这是代码:

class BaseView(APIView):
  def handle_exception(self, exc):
     if isinstance(exc, MYEXCEPTION):
        self.headers['WWW-Authenticate'] = "Token"
        return Response({'detail': exc.detail,
                        status=exc.status_code, exception=True)

Your thoughts?你的意见?

Try overriding finalize_response in your rest framework view:尝试在您的休息框架视图中覆盖finalize_response

def finalize_response(self, request, *args, **kwargs):
    response = super(SomeAPIView, self).finalize_response(request, *args, **kwargs)
    response['WWW-Authenticate'] = 'Token'
    return response

Edit:编辑:

After seeing your update, I think your override of handle_exception should work, I would only add an else statement to call the parent method to cover other exceptions.看到你的更新后,我认为你对handle_exception的覆盖应该可以工作,我只会添加一个 else 语句来调用父方法来覆盖其他异常。 One thing I noticed in overriding dispatch, which may not be an issue here, is that setting a new key/value for self.headers resulted in a server error that I didn't take the time to track down.我在覆盖 dispatch 时注意到的一件事(这里可能不是问题)是为 self.headers 设置新的键/值会导致服务器错误,我没有花时间去追踪。 Anyways, it seems you are on the right track.不管怎样,看来你是在正确的轨道上。

Use the authenticate_header method on your authentication class.在您的身份验证类上使用authenticate_header方法。

Additionally that'll ensure your responses also have the right 401 Unauthorized status code set, instead of 403 Forbidden .此外,这将确保您的响应也具有正确的401 Unauthorized状态代码集,而不是403 Forbidden

See here: http://django-rest-framework.org/api-guide/authentication.html#custom-authentication请参阅此处: http : //django-rest-framework.org/api-guide/authentication.html#custom-authentication

Your solution is quite correct, in my case I found it more appropiate to add the header and then call the method on the super instance, to maintain default behaviour:您的解决方案非常正确,在我的情况下,我发现添加标头然后在超级实例上调用该方法更合适,以保持默认行为:

class BaseView(APIView):
  def handle_exception(self, exc):
     if isinstance(exc, MYEXCEPTION):
        self.headers['WWW-Authenticate'] = "Token"
        return super().handle_exception(excepto)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM