Return method not allowed error in JSON in some situations

Added this while playing with the new API explorer, refs #1871
This commit is contained in:
Simon Willison 2022-11-02 20:12:13 -07:00
commit 719e757252

View file

@ -69,20 +69,26 @@ class BaseView:
def database_color(self, database):
return "ff0000"
async def options(self, request, *args, **kwargs):
async def method_not_allowed(self, request):
print(request.headers)
if request.path.endswith(".json") or request.headers.get("content-type") == "application/json":
return Response.json({"ok": False, "error": "Method not allowed"}, status=405)
return Response.text("Method not allowed", status=405)
async def options(self, request, *args, **kwargs):
return await self.method_not_allowed(request)
async def post(self, request, *args, **kwargs):
return Response.text("Method not allowed", status=405)
return await self.method_not_allowed(request)
async def put(self, request, *args, **kwargs):
return Response.text("Method not allowed", status=405)
return await self.method_not_allowed(request)
async def patch(self, request, *args, **kwargs):
return Response.text("Method not allowed", status=405)
return await self.method_not_allowed(request)
async def delete(self, request, *args, **kwargs):
return Response.text("Method not allowed", status=405)
return await self.method_not_allowed(request)
async def dispatch_request(self, request):
if self.ds: