- 1. FastAPI 입문: 설치부터 Hello World 까지
- 2. Path 와 Query 매개변수 (URL 로 데이터 받기)
- 3. Pydantic 으로 Request Body 다루기
- 4. Response Model 과 HTTP 상태 코드
- 5. 예외 처리 (HTTPException 완벽 가이드)
- 6. 의존성 주입 (Dependency Injection) 정복
- 7. SQLAlchemy 비동기 데이터베이스
- 8. JWT 인증과 보안 (로그인부터 토큰 갱신까지)
- 9. 실시간 통신 (WebSockets & SSE)
- 10. 테스트 작성법 (Pytest 완전 가이드)
4. Response Model 과 HTTP 상태 코드
# 4 강. Response Model 과 HTTP 상태 코드
## 🎯 학습 목표
- Response Model 로 응답 데이터 제어
- HTTP 상태 코드 설정
- 민감 정보 필터링
---
## 1. Response Model
```python
class UserCreate(BaseModel):
username: str
password: str # 입력용
class UserResponse(BaseModel):
id: int
username: str
# password 없음! ✅
@app.post("/users", response_model=UserResponse)
def create_user(user: UserCreate):
return user # ✅ password 자동 필터링
```
---
## 2. HTTP 상태 코드
```python
from fastapi import FastAPI, status
app = FastAPI()
# 201 Created
@app.post("/items", status_code=status.HTTP_201_CREATED)
def create_item(name: str):
return {"name": name}
# 204 No Content
@app.delete("/items/{id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_item(id: int):
return None
```