Fastapi: Can we create multiple endpoits for the list of endpoints..

Created on 4 Aug 2020  路  2Comments  路  Source: tiangolo/fastapi

First check

  • [ ] I added a very descriptive title to this issue.
  • [ ] I used the GitHub search to find a similar issue and didn't find it.
  • [ ] I searched the FastAPI documentation, with the integrated search.
  • [ ] I already searched in Google "How to X in FastAPI" and didn't find any information.
  • [ ] I already read and followed all the tutorial in the docs and didn't find an answer.
  • [ ] I already checked if it is not related to FastAPI but to Pydantic.
  • [ ] I already checked if it is not related to FastAPI but to Swagger UI.
  • [ ] I already checked if it is not related to FastAPI but to ReDoc.
  • [ ] After submitting this, I commit to:

    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.

    • Or, I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.

    • Implement a Pull Request for a confirmed bug.

Environment

  • OS: [e.g. Linux / Windows / macOS]: windows
  • 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


question

Most helpful comment

Thanks for the help here @makupi ! :clap: :bow:

Thanks for reporting back and closing the issue :+1:

All 2 comments

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:

Was this page helpful?
0 / 5 - 0 ratings