fastapi get header parameter is none in basemodel use Header cant get token but in function its ok why?

Viewed 471

in basemodel use Header cant get token but in function its ok why?

   class Test(BaseModel):
        name: str
        token: str= Header(None)

def login(t: Test):
    print(t.dict())
    return 'test'

output  {'name': '123', 'token': None} 

if i do this is ok

def login(t: Test,token: str= Header(None)):
        print(t.dict(),token)
        return 'test'
    
    output  {'name': '123', 'token': None} 123456

who can help me plz !

1 Answers

The Pydantic model here represents the validation of the request payload.

The values mentioned in the "Test" model should be available in request payload. Since the header is not the payload, we cannot use that in Basemodel.

Yet you can use that in api function. When you call the api, then the request params can be used in the function. So, it is possible to use this

Related