{"detail":[{"loc":["body"],"msg":"field required","type":"value_error.missing"}]}

今天用fastapi遇到了一个错误记录一下,代码如下:

from typing import *
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

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

@app.get("/user")
def hello(user:User):
    return {"username":user.username, "password": user.password}

原因是get请求和head请求不能够这样放对象进入好像,用其他请求方式就可以了。改为:

@app.get("/user")
def hello(username:str, password:str):
	return {"username":username, "password":password}

或者:

@app.post("/user")
def hello(user:User):
    return {"username":user.username, "password": user.password}
posted @ 2022-09-09 09:57  小默同学  阅读(4604)  评论(0编辑  收藏  举报