I am trying to create a token with FastAPI:
> import json
> import os
> import aiohttp
> import asyncio
> from fastapi import FastAPI
> from fastapi import APIRouter, Request
> from fastapi.responses import JSONResponse
>
> token = APIRouter(prefix="/management/api", tags=["API Apl token"])
>
> app=FastAPI()
> app.include_router(token)
and many methods after...
I got this
> uvicorn peg:token --reload
> INFO: Will watch for changes in these directories: ['C:\\Users\\Ejbc25\\fastapi']
> INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
> INFO: Started reloader process [2232] using StatReload
> INFO: Started server process [20632]
> INFO: Waiting for application startup.
> INFO: Application startup complete.
> INFO: 127.0.0.1:51183 - "GET / HTTP/1.1" 404 Not Found
> INFO: 127.0.0.1:51183 - "GET / HTTP/1.1" 404 Not Found
How to fix this problem?
I expected that http://127.0.0.1:8000/token would return token but shows
Not Found
The reason of your issue is that you're trying to run a FastAPI APIRouter
instance (token
) directly with uvicorn
, instead of the actual FastAPI
application (app
).
Your uvicorn
command:
uvicorn peg:token --reload
is trying to start the app using the token
router as the main app, which won’t work since token
is not a FastAPI application (it’s just a router meant to be included in an app).
You should point uvicorn
to the actual FastAPI
instance, which in your code is named app
. Change your command to:
uvicorn peg:app --reload
Obviously I'm assuming that the your file is named peg.py
, but if it has a different name update the command (e.g., uvicorn main:app --reload
for main.py
).