{"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}