더북(TheBook)

FastAPI는 쿼리 파라미터를 엔드포인트 함수의 매개변수로 받아 사용하며, 타입 힌트로 기본값을 지정할 수 있습니다. 예를 들어 상품의 최대 가격을 쿼리 파라미터(max_price=1000)로 전달했다면 다음과 같이 조회할 수 있습니다.

hello_fastapi/main.py

# 경로 변수 사용
@app.get("/users/{user_id}")
def read_user_handler(user_id: int):
    return {"user_id": user_id, "message": f"사용자 {user_id} 정보 조회"}

# 쿼리 파라미터 사용 
@app.get("/items")
def read_items_handler(max_price: int):
    return {"max_price": max_price}

read_items_handler() 함수를 보면 max_price: int로 타입 힌트가 적용되어 있습니다. 따라서 FastAPI가 max_price의 값이 정수인지 자동으로 검증합니다.