我正在尝试使用Pydantic在我的Starlette应用程序中获得用户价值。我怎样做呢?
class Post(BaseModel):
title:str
content:str
@app.route("/createposts",methods=["POST"])
async def create_posts(request):
data = await request.json()
print(data)
return JSONResponse({"data":data})发布于 2022-05-25 08:04:25
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from pydantic import BaseModel
from pydantic import ValidationError
from typing import Optional
app = Starlette()
class Post(BaseModel):
title:str
content:str
rating:Optional[int]=None
@app.route("/createposts",methods=["POST"])
async def create_posts(request):
data = await request.json()
try:
parsed_post = Post(**data)
except ValidationError as e:
return JSONResponse({"error":e.json()})
return JSONResponse({"post_data":"Post Create Successfully"})谢谢你@monkut
https://stackoverflow.com/questions/72371702
复制相似问题