FastAPI上传POST嵌套JSON对象及List列表BaseModel,python
Posted zhangphil
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FastAPI上传POST嵌套JSON对象及List列表BaseModel,python相关的知识,希望对你有一定的参考价值。
FastAPI上传POST嵌套JSON对象及List列表BaseModel,python
from typing import Optional, Union, List
import uvicorn
from fastapi import FastAPI, Body
from pydantic import BaseModel, HttpUrl
app = FastAPI()
class Image(BaseModel):
name: str
url: HttpUrl
class Person(BaseModel):
name: str
desc: Optional[str] = None
year: List[int]
img: Union[List[Image], None] = None
@app.post("/item")
async def create_item(person: Person = Body(embed=True)):
return person
if __name__ == '__main__':
uvicorn.run(app=app, host="0.0.0.0", debug=True)
以上是关于FastAPI上传POST嵌套JSON对象及List列表BaseModel,python的主要内容,如果未能解决你的问题,请参考以下文章
FastAPI上传POST多个对象BaseModel数据JSON,python
FastAPI学习-6.POST请求 JSON 格式 body