Created
May 12, 2025 22:26
-
-
Save swilcox/33979777011a059decb64eb8ff1d7c8a to your computer and use it in GitHub Desktop.
litestar pydantic post endpoint model validation?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pydantic import BaseModel | |
from litestar import Litestar, get, post | |
class Book(BaseModel): | |
id: int | |
title: str | |
author: str | |
@get("/") | |
async def index() -> str: | |
return "hello, world" | |
@get("/books/{book_id:int}") | |
async def get_book(book_id: int) -> Book: | |
return Book(id=book_id, title="My Book", author="S.C. Wilcox") | |
@post("/books") | |
async def post_book(data: Book) -> Book: | |
return data | |
app = Litestar([index, get_book, post_book]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment