【FastAPI】クエリパラメータを使ってみる

単一のクエリパラメータ

from fastapi import FastAPI

app = FastAPI()

@app.get("/hello")
def hello(name):
    return f"こんにちは、{name}さん"

http://localhost:8000/hello?name=odaneko

複数のクエリパラメータ

from fastapi import FastAPI

app = FastAPI()

@app.get("/hello")
def hello(name, message):
    return f"{message}{name}さん"

http://localhost:8000/hello?name=odaneko&message=hello

任意のクエリパラメータ

from fastapi import FastAPI

app = FastAPI()

@app.get("/hello")
def hello(name, message = "hello"):
    return f"{message}{name}さん"

messageを省略しない場合
http://localhost:8000/hello?name=odaneko&message=hey

messageを省略した場合
http://localhost:8000/hello?name=odaneko

以上になります。
お読み頂き、ありがとうございました。