[Python] Detail not found error using FastAPI's APIRouter

Discussão em 'Python' iniciado por Stack, Setembro 2, 2025.

  1. Stack

    Stack Membro Participativo

    I have a directory structure as follows:

    app
    >routers
    >items.py
    __init__.py
    main.py


    Inside main I have the following code:

    from typing import Union
    import uvicorn
    from fastapi import FastAPI, APIRouter
    from routers import items



    app = FastAPI()
    app.include_router(items.router, prefix='/items', tags=['items'])

    @app.get("/")
    async def root():
    return {"message": "World World"}

    if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)


    Within items.py I have the following:

    from fastapi import APIRouter

    router = APIRouter(
    prefix="/items",
    tags=["items"]
    )

    @router.api_route("/items")
    async def items():
    return {"test": "items"}


    When I run the code, I can go to my url http:127.0.0.0:8000/ and I get the Hello world message. But when i go to http:127.0.0.0:8000/items I'm seeing an error:

    {"detail": "not found"}


    How do I fix this? I tried debugging this but when I hit my debugger, and type items.router it tells me that I'm correctly importing from the right path.

    Continue reading...

Compartilhe esta Página