Pydantic Models for Forms
Use Body(..., embed=True) with Pydantic models for complex form data:
from pydantic import BaseModel
from justapi import JustAPIApp, Form
class LoginForm(BaseModel):
username: str
password: str
remember_me: bool = False
app = JustAPIApp()
@app.post("/login")
def login(form: LoginForm = Form(...)):
return {"username": form.username, "remember_me": form.remember_me}
:::tip
When using Pydantic models with Form(...), the embed=True parameter is required by Pydantic to correctly parse the form fields.
:::
File + Form Combination
from justapi import JustAPIApp, Form, UploadFile
class CreateItemWithImage(BaseModel):
name: str
description: str = ""
app = JustAPIApp()
@app.post("/items/")
async def create_item(
item: CreateItemWithImage = Form(...),
image: UploadFile = File(...),
):
return {"item": item.model_dump(), "filename": image.filename}
See Also
- Form Data — basic form fields
- Request Files — file uploads
- Request Body — JSON request bodies