79263175

Date: 2024-12-08 19:15:58
Score: 0.5
Natty:
Report link

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
Reasons:
  • Blacklisted phrase (0.5): thanks
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: EagleStar