[Python] FastAPI / uvicorn (or hypercorn): where is my root-path?

Discussão em 'Python' iniciado por Stack, Fevereiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    Based on a few FastAPI tutorials, including this, I made a simple FastAPI app:

    from fastapi import FastAPI, Request
    app = FastAPI() # also tried FastAPI(root_path="/api/v1")

    @app.get("/app")
    def read_main(request: Request):
    return {"message": "Hello World", "root_path": request.scope.get("root_path")}


    Which I want to have at a path other than root (e.g. /api/vi). Again based on most tutorials and common sense, I tried to start it with e.g.:

    uvicorn main:app --root-path /api/v1


    The service comes up ok (on http://127.0.0.1:8000), however, the root-path seems to be ignored, i.e., any GET request to http://127.0.0.1:8000/ gives:

    message "Hello World"
    root_path "/api/v1"


    and any GET request to http://127.0.0.1:8000/api/v1 gives:

    detail "Not Found"


    I would expect the requests to produce the reverse outcomes. What is going on here?!

    I also tried initializing FastAPI with FastAPI(root_path="/api/v1"), as well as switching to hypercorn without avail.

    Details of the versions of apps (I might have tried a few others as well, though these should be the latest tried):

    python 3.9.7 hf930737_3_cpython conda-forge
    fastapi 0.85.1 pyhd8ed1ab_0 conda-forge
    uvicorn 0.20.0 py39h06a4308_0
    hypercorn 0.14.3 py39hf3d152e_1 conda-forge

    Continue reading...

Compartilhe esta Página