더북(TheBook)

일반적으로 응답 본문을 반환할 때는 상태 코드도 함께 명시하는 것이 좋습니다. 이를 위해 FastAPI에서는 데코레이터에 status_code 옵션으로 상태 코드를 지정합니다. # 새 아이템 등록 코드를 다음과 같이 수정한 후 저장합니다.

hello_fastapi/main.py

from fastapi import FastAPI, status     # ➊ status 모듈 임포트
(중략)

 # 새 아이템 등록
@app.post(
    "/items",
    response_model=Item,
    status_code=status.HTTP_201_CREATED  # ➋ 성공 시 반환할 상태 코드 지정
)
def create_item_handler(item: Item):
    return item