How to create Request samples for Form input in FastAPI?

217 Views Asked by At

I wish to create sample requests for my API documentation in Swagger UI, but I am accepting Form input.

FastAPI docs demonstrate how to do that only for Pydantic schema. Please help me with this

Can example/examples be used for Form input to create sample requests in any way?

1

There are 1 best solutions below

0
Deepak On

You need to pass the pydantic class in api route function image

image

from fastapi import FastAPI, Form
from pydantic import BaseModel

app = FastAPI()

class request_json(BaseModel):
    name:str
    age:int
    mail:str
        
@app.get("/name")
async def get_values(
    reqest_model:request_json,
    username: Annotated[str, Form()],
    password: Annotated[str, Form()]
):
    responce_dict = {"name":reqest_model.name, "username": username}}
    return responce_dict