[Python] How can I add unknown arguments in a POST request in FastAPI using BaseModel?

Discussão em 'Python' iniciado por Stack, Outubro 5, 2024.

  1. Stack

    Stack Membro Participativo

    I have the following code:

    from fastapi import FastAPI
    from pydantic import BaseModel

    app = FastAPI()


    class Request(BaseModel):
    user_name: str
    age: int
    # other unknown arguments


    @app.post("/home")
    def write_home(request: Request):
    print(request.__dict__)
    return {
    "user_name": request.user_name,
    "age": request.age,
    # other arguments...
    }


    I would like the request to take optional arguments (like height, weight, etc) but these arguments may be unknown.

    Thanks in advance

    I tried to add them directly in the request but it doesn't print out other unspecified arguments [​IMG]

    Continue reading...

Compartilhe esta Página