fastapi logo fastapi: json

1 2 3 4 5 6 7 8 9
Notes

You can send json to FastApi. This is like sending a python dictionary. You'd still like the dictionary you send to adhere to a standard though. FastApi using the pydantic library to help you define the perfect json type.

The app.py file at the end of our video looks like this;

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def root():
    return {"message": "hello world again"}

@app.get("/users/{user_id}")
def read_user(user_id: str):
    return {"user_id": user_id}

from pydantic import BaseModel, validator

class Item(BaseModel):
    name: str
    price: float

@app.post("/items/")
def create_item(item: Item):
    return item

Don't forget that you need to import the validator decorator from pydantic.