Is there an easier way to allow all http methods in Sanic framework?

262 Views Asked by At

I'm developing a proxy server by using Sanic. (The reason why I chose this framework is that it is so fast and it uses asynchronous event loop.)

So I want to catch all requests to my Sanic server and do some post-processings.

@app.route("/<path:path>", methods = []) # Here is the question point
async def proxy(request: Request, path: str):
   // do something
  return redirect("...")

I think it is not so good to write all HTTP methods and pass them through methods parameter. So I want to know if there is a more effective way to allow all HTTP methods to my route.

Thanks in advance.

  • Python version: 3.11
  • Sanic version: 23.3.0
2

There are 2 best solutions below

0
jeongmin.cha On BEST ANSWER

** Answering my own question

I just have to get all values of the enum HTTPMethod. I don't understand that I didn't come up with such an easy solution.

@app.route("/<path:path>", methods = list(map(str, HTTPMethod)))
async def proxy(request: Request, path: str):
   // do something
  return redirect("...")
2
RAI On

not sure why you need it but here are Sanic docs. Routing that will explain to you more in detail. In short, Sanic will automatically allow all non-safe HTTP methods for the route if you omit methods