Calmcode - fastapi: json

How to receive JSON in FastAPI.

1 2 3 4 5 6 7 8 9

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 file at the end of our video looks like this;

from fastapi import FastAPI

app = FastAPI()

def root():
    return {"message": "hello world again"}

def read_user(user_id: str):
    return {"user_id": user_id}

from pydantic import BaseModel, validator

class Item(BaseModel):
    name: str
    price: float"/items/")
def create_item(item: Item):
    return item

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