Skip to content

Body Unions

You can accept a Union of bodies, which will be resolved by trying to deserialize each body and returning the first one that does not error. If your bodies accept only specific content types (this is the default for json bodies but is opt-in for files) this will be used to discriminate the type. If no bodies verify successfully, an error will be returned to the client.

from typing import Dict, Optional, Union

from pydantic import BaseModel

from xpresso import App, FromBodyUnion, FromFormData, FromJson, Path

class Item(BaseModel):
    name: str
    price: float
    tax: Optional[float] = None

async def create_receipt(
    item: FromBodyUnion[Union[FromFormData[Item], FromJson[Item]]]
) -> Dict[str, float]:
    return { item.price + ( or 0)}

app = App(