首页 > 其他分享 >[FastAPI-21]响应体

[FastAPI-21]响应体

时间:2023-03-25 12:34:18浏览次数:41  
标签:return 21 FastAPI app 响应 User login user

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

标签:return,21,FastAPI,app,响应,User,login,user
From: https://www.cnblogs.com/leoshi/p/17254503.html

相关文章