[FastAPI-21]响应体

发布时间 2023-03-25 12:26:05作者: LeoShi2020
from fastapi import FastAPI, Response
from fastapi.responses import JSONResponse
from pydantic import BaseModel

app = FastAPI()


class User(BaseModel):
    username: str
    password: str

'''
响应体 Response
返回的数据是将数据转换为JSON格式
'''

@app.get("/")
def hello():
    return [{"id":i,"name":f"图书{i}"}for i in range(10)]

# @app.post("/login")
# def login(user:User):
#     # 将数据转换为字典格式返回
#     return user.dict()

@app.post("/login")
def login(user:User):
    response = JSONResponse(
        content="123456"
    )
    return response