docs_src/response_directly/tutorial001_py310.py
raw| 1 | from datetime import datetime |
| 2 | |
| 3 | from fastapi import FastAPI |
| 4 | from fastapi.encoders import jsonable_encoder |
| 5 | from fastapi.responses import JSONResponse |
| 6 | from pydantic import BaseModel |
| 7 | |
| 8 | |
| 9 | class Item(BaseModel): |
| 10 | title: str |
| 11 | timestamp: datetime |
| 12 | description: str | None = None |
| 13 | |
| 14 | |
| 15 | app = FastAPI() |
| 16 | |
| 17 | |
| 18 | @app.put("/items/{id}") |
| 19 | def update_item(id: str, item: Item): |
| 20 | json_compatible_item_data = jsonable_encoder(item) |
| 21 | return JSONResponse(content=json_compatible_item_data) |