February 22, 2025

Get Any form of Query

Request has all information thru

@router.get('/data')
def data_get(
    req: fastapi.Request
):
    query = dict(req.query_params)
    return query

Combination

from fastapi import FastAPI, Depends
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str
    description: str
    price: float
    tax: float | None = None


def get_item(item: Item):
    return item

def calculate_tax(item: Item):
    total_price = item.price + (item.tax or 0)
    return {"total_price": total_price, "name": item.name}

@app.post("/items")
async def create_item(
    item: Item = Depends(get_item),
    tax_info: dict = Depends(calculate_tax)
):
    return {
        "item": item,
        "calculated": tax_info
    }