| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from dataclasses import dataclass |
| 4 |
|
| 5 |
from typing_extensions import Annotated |
| 6 |
|
| 7 |
from litestar import Litestar, post |
| 8 |
from litestar.dto import DataclassDTO, DTOConfig |
| 9 |
|
| 10 |
|
| 11 |
@dataclass |
| 12 |
class User: |
| 13 |
id: str |
| 14 |
|
| 15 |
|
| 16 |
UserDTO = DataclassDTO[Annotated[User, DTOConfig(forbid_unknown_fields=True)]] |
| 17 |
|
| 18 |
|
| 19 |
@post("/users", dto=UserDTO) |
| 20 |
async def create_user(data: User) -> User: |
| 21 |
return data |
| 22 |
|
| 23 |
|
| 24 |
app = Litestar([create_user]) |
| 25 |
|
| 26 |
|
| 27 |
# run: /users -H "Content-Type: application/json" -d '{"id": "1", "name": "Peter"}' |