Skip to content

Instantly share code, notes, and snippets.

@swilcox
Created May 12, 2025 22:26
Show Gist options
  • Save swilcox/33979777011a059decb64eb8ff1d7c8a to your computer and use it in GitHub Desktop.
Save swilcox/33979777011a059decb64eb8ff1d7c8a to your computer and use it in GitHub Desktop.
litestar pydantic post endpoint model validation?
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