python - Adding custom response Headers to APIException -
i have created custom exception referring http://django-rest-framework.org/api-guide/exceptions.html.
please know have own authentication backend. hence not using rest_framework's authentication module.
for authentication errors, want add 'www-authenticate: token' header response sent exception.
any ideas helpful.
update:
thanks @pathétique, ended doing.
-have base view class named baseview.
-override handle_exception method set appropriate headers, in case 'www-authenticate'.
here 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 rest framework view:
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 update, think override of handle_exception
should work, add else statement call parent method cover other exceptions. 1 thing noticed in overriding dispatch, may not issue here, setting new key/value self.headers resulted in server error didn't take time track down. anyways, seems on right track.
Comments
Post a Comment