Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't access my router in fastapi with postman

If I try to access my router(/users/signup)
with postman(send to http://127.0.0.1:8000/users/signup), it keeps the response "detail": "Not Found"
and fastapi is showing
api | INFO: 172.19.0.1:43736 – "GET /users/signup HTTP/1.1" 404 Not Found

Originally I tried to use post instead of get but it doesn’t work anyway

Here is the code in my main.py in the app folder and user.py in the routers folder

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

main.py:

import os
import sys
from .database import init_db
sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
baseurl = os.path.dirname(os.path.abspath(__file__))
from fastapi import FastAPI, APIRouter
from .routers.user import router as user_router
from .routers.article import router as article_router
from fastapi.middleware.cors import CORSMiddleware


router = APIRouter()
router.include_router(user_router, prefix="/users",tags=["users"])
router.include_router(article_router, prefix="/articles",tags=["articles"])
app = FastAPI()
origins = ["http://localhost:3000"]
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.on_event("startup")
async def on_startup():
    await init_db()
@app.get("/")
async def root():
    return {"message ": " Welcome Fastapi"}

@app.get("/hello/{name}")
async def say_hello(name: str):
    return {"message": f"Hello {name}"}

user.py

from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session

import app.repository.user as dao
from app.admin.utils import currentTime
from app.database import get_db
from app.schemas.user import UserDTO
from app.admin.utils import currentTime

router = APIRouter()


@router.get("/signup")
async def signup(user: UserDTO, db: Session = Depends(get_db)):
    print(f" 회원가입에 진입한 시간: {currentTime()} ")
    print(f"SignUp Inform : {user}")
    result = dao.signup(user, db)
    if result == "":
        result = "failure"
    return {"data": result}


@router.post("/login")
async def login(user: UserDTO, db: Session = Depends(get_db)):
    return_user = dao.login(user, db)
    print(f"로그인 정보 : {return_user}")
    return {"data": return_user}


@router.put("/modify/{id}")
async def update(id: str, item: UserDTO, db: Session = Depends(get_db)):
    dao.update(id, item, db)
    return {"data": "success"}


@router.delete("/delete/{id}", tags=['age'])
async def delete(id: str, item: UserDTO, db: Session = Depends(get_db)):
    dao.delete(id, item, db)
    return {"data": "success"}


@router.get("/page/{page}")
async def get_users(page: int, db: Session = Depends(get_db)):
    ls = dao.find_users(page, db)
    return {"data": ls}


@router.get("/email/{id}")
async def get_user(id: str, db: Session = Depends(get_db)):
    dao.find_user(id, db)
    return {"data": "success"}


@router.get("/point/{search}/{page}")
async def get_users_by_point(search: int, page: int, db: Session = Depends(get_db)):
    dao.find_users_by_point(search, page, db)
    return {"data": "success"}

What I was expecting is send {"user_email": "hong@naver.com", "id": "hong1234", "username": "홍길동", "password": "hong1234", "cpassword": "hong1234"} by using postman and recieve data:success and enrollment the data to DB

I tried the post method and get method and change router address but it didn’t work anyway

>Solution :

You need to include router in your app too:

router = APIRouter()
router.include_router(user_router, prefix="/users",tags=["users"])
router.include_router(article_router, prefix="/articles",tags=["articles"])
app = FastAPI()
app.include_router(router) # Add this.

Alternatively remove main router:

# Remove router = APIRouter()
app = FastAPI()
app.include_router(user_router, prefix="/users",tags=["users"])
app.include_router(article_router, prefix="/articles",tags=["articles"])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading