Complete the code to define a POST endpoint that accepts a JSON body.
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str price: float @app.post("/items/") async def create_item(item: [1]): return item
The endpoint expects an Item object from the request body, which is a structured data model.
Complete the code to access the 'name' field from the request body model.
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class User(BaseModel): username: str age: int @app.post("/users/") async def create_user(user: User): return {"username": user.[1]
The username field is accessed from the User model instance.
Fix the error in the code by completing the parameter type to accept structured JSON data.
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Product(BaseModel): title: str quantity: int @app.post("/products/") async def add_product(product: [1]): return product
The parameter must be typed as the Pydantic model Product to parse structured JSON data.
Fill both blanks to create a Pydantic model with fields 'email' and 'is_active'.
from pydantic import BaseModel class UserStatus(BaseModel): email: [1] is_active: [2]
The email field is a string and is_active is a boolean.
Fill all three blanks to define a POST endpoint that accepts a structured request body and returns a dictionary with the user's name and age.
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Person(BaseModel): name: [1] age: [2] @app.post("/persons/") async def create_person(person: [3]): return {"name": person.name, "age": person.age}
The name is a string, age is an integer, and the endpoint parameter uses the Person model.