2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

FastAPIで、すべてのパスのリクエストを拾う方法

Posted at

初めに

flaskで、

@app.route('/<path:path>', methods=['GET', 'POST'])
def catch_all(path):
    # print(path)
    return {"path": path}

とかやると、全パス拾えるけど、FastApiでは微妙にやり方が違っていた話

方法

from fastapi import FastAPI

app = FastAPI()

@app.api_route("/{path:path}", methods=['GET', 'POST'])
def catch_all(path: str):
    # print(path)
    return {"path": path}

最後に

URLとフォルダのパス紐づけたいとかそういう時に使えるかと。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?