I have a method decorated with the following decorators:
@request.restful()
@auth.requires_login()
On an unauthenticated (non-logged-in) request, I get a 403 response with "Not authorized" in the response body, as expected per default settings. I would like to be able to modify this response to be JSON. I've tried modifying the following auth.settings as follows:
def json_response():
import json
raise HTTP(403, json.dumps(dict(content='Not authorized')))
auth.settings.on_failed_authentication = json_response
auth.settings.on_failed_authorization = json_response
However, I continue to receive the "Not authorized" string in the response to the RESTful method instead of the JSON. Upon further digging, I found line 3448 in gluon/tools.py with the following:
elif self.settings.allow_basic_login_only or \
basic_accepted or current.request.is_restful:
raise HTTP(403, "Not authorized")
It seems like methods with the @request.restful() decorator simply cannot be overridden to return a custom response on unauthenticated requests, at least without modifying tools.py. Is this correct?
Thanks ahead of time for any help.