To parse the body, it is indeed necessary to create some classes thanks to pydantic to achieve my goal. Here is the final code.
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
msgs=dict()
class Body(BaseModel):
title: str
@app.post("/posts/")
async def posts(body: Body):
number= len(msgs)+1
title = body
msgs[number]= {
"type": "PostCreated",
"data": {
"id": number,
"title": title.title
}
}
return msgs
@app.get("/")
async def root():
return msgs