Cơ BảnFastAPI iconFastAPI

Request body, path params và query params trong FastAPI được phân biệt thế nào?

FastAPI phân biệt dựa trên vị trí khai báo và type: parameter có trong path template là path param; primitive parameter không nằm trong path thường là query param; Pydantic model thường là request body.

Ví dụ:

python
@app.post("/users/{user_id}")
async def update_user(user_id: int, notify: bool = False, payload: UserUpdate = Body()):
    return {"id": user_id, "notify": notify, "payload": payload}

Trong API public, nên đặt validation constraints rõ ràng bằng Annotated, Path, Query, Body để docs và lỗi 422 chính xác.

Xem toàn bộ FastAPI cùng filter theo level & chủ đề con.

Mở danh sách FastAPI