Answers and Bodies
Answers and Bodies: When It Isn't There: HTTPException and 404, Taking JSON In: Your First Request Body.
Updated
- When It Isn't There: HTTPException and 404 (Apprentice): A FastAPI route answers 404 for an item that isn't there by raising HTTPException with a status code and a detail message, which stops the function at once and sends {"detail": ...}, and a route that creates something sets status_code=201 on its decorator.
- Taking JSON In: Your First Request Body (Apprentice): A POST route takes the JSON a request sends in its body through a small Pydantic model, a class that names each field and its type, then turns it into a dictionary with model_dump(), gives the new item the next id, and answers 201 with it.