FastAPI uses structured data like JSON to automatically check if the data matches expected types and shapes. This helps catch errors early and makes it easy to work with the data as Python objects.
FastAPI automatically parses and validates the JSON body against the Pydantic model, then provides a Python object to the endpoint function for easy use.
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class User(BaseModel): name: str age: int @app.post("/user") def create_user(user: User): return {"user_name": user.name}
The code uses @app.post to accept POST requests with JSON matching the User model. The function returns the user's name correctly.
The id field is sent as a string "123" but the model expects an int. FastAPI's validation fails because of this type mismatch.
The endpoint returns the city from the nested address and the user's name as provided in the JSON body.
