Fast API Request Body¶
Info
Sending data to server/client, the data is sent in the request body.
- API almost has to send response body.
- Client don't have to send often request body.
Datamodel Creation¶
Note
Datamodels are created with pydantic
library.
Same logic with optional, default, required
parameters.
from fastapi import FastAPI
from pydantic import BaseModel
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI()
@app.post("/items/")
async def create_item(item: Item):
return item
Response
{
"name": "Foo",
"description": "An optional description",
"price": 45.2,
"tax": 3.5
},
{
"name": "Foo",
"price": 45.2
}
Data Model Usage¶
Tip
Item
Datamodel dict can be updated with new key-value pairs.
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI()
@app.post("/items/")
async def create_item(item: Item):
item_dict = item.dict()
if item.tax:
price_with_tax = item.price + item.tax
item_dict.update({"price_with_tax": price_with_tax})
return item_dict
Request Body & Path & Query Parameters¶
Info
FasAPI detects/defines all parameters name, also in Datamodels.
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI()
@app.put("/items/{item_id}")
async def create_item(item_id: int, item: Item, q: str | None = None):
result = {"item_id": item_id, **item.dict()}
if q:
result.update({"q": q})
return result