Tag Archives: fastapi error

[Solved] fastapi error: {“detail”:[{“loc”:[“body”],”msg”:”field required”,”type”:”value_error.missing”}]}

Today, I encountered an error with fastapi and recorded it. The code is as follows:

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}

The reason is that the get request and the head request cannot put the object into it like this, and other request methods can be used. Change it to:

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

or:

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