From 719e75725240f75b994cf22bf3f6d1a502bb3d07 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Wed, 2 Nov 2022 20:12:13 -0700 Subject: [PATCH] Return method not allowed error in JSON in some situations Added this while playing with the new API explorer, refs #1871 --- datasette/views/base.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/datasette/views/base.py b/datasette/views/base.py index d8fe71d0..fcf05251 100644 --- a/datasette/views/base.py +++ b/datasette/views/base.py @@ -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: