FastAPI Version [e.g. 0.3.0]: 0.44.0
Python version: 3.7.5
Lets say I have a list of endpoints for my app:
endpoints = ["/endpoint1", "/endpoint2" , "/endpoint2" ]
and need to create multiple endpoints dynamically like below...
@app.get("/endpoint1")
async def func1():
data = {
"status": 200,
"endpoint_no" : "endpoint1"
}
return data
@app.get("/endpoint2")
async def func2():
data = {
"status": 200,
"endpoint_no" : "endpoint2"
}
return data
@app.get("/endpoint3")
async def func3():
data = {
"status": 200,
"endpoint_no" : "endpoint3"
}
return data
@app.get("/endpoint4")
async def func4():
data = {
"status": 200,
"endpoint_no" : "endpoint4"
}
return data
@app.get("/endpoint5")
async def func5():
data = {
"status": 200,
"endpoint_no" : "endpoint5"
}
return data
You could do something like this:
@app.get("/endpoint{endpoint_number}")
async def func(endpoint_number: int):
data = {
"status": 200,
"endpoint_no" : f"endpoint{endpoint_number}"
}
return data
But I'm not sure if that helps in your actual use case. What do you need this list-endpoint functionality for?
Thanks for the help here @makupi ! :clap: :bow:
Thanks for reporting back and closing the issue :+1:
Most helpful comment
Thanks for the help here @makupi ! :clap: :bow:
Thanks for reporting back and closing the issue :+1: