Sometimes, you may want to set multiple values to a query parameter. For example,
@app.get("/emps")
def empsInfo(
ids : Optional[List[int]] = Query(None),
)
Above snippet, can accept below url.
http://127.0.0.1:8000/emps?ids=1&ids=2&ids=3
The interactive API docs will update accordingly, to allow multiple values.
Find the below working application.
queryParamMultiValues.py
from fastapi import FastAPI, Query
from typing import List, Optional
app = FastAPI()
@app.get("/emps")
def empsInfo(
ids : Optional[List[int]] = Query(None),
):
return {"ids" : ids}
Open terminal and execute the command ‘uvicorn queryParamMultiValues:app --reload’.
$uvicorn queryParamMultiValues:app --reload
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [55485] using statreload
INFO: Started server process [55487]
INFO: Waiting for application startup.
INFO: Application startup complete.
Open the url ‘http://127.0.0.1:8000/docs’ in browser and experiment with swagger documentation.
Previous Next Home
No comments:
Post a Comment