Search code examples
fastapi

Form Input Missing in FastAPI, Displays JSON Instead During Testing


I am trying out the examples from FastAPI: FastAPI > Learn > Tutorial-User Guide > Form Models

from typing import Annotated

from fastapi import FastAPI, Form
from pydantic import BaseModel

app = FastAPI()


class FormData(BaseModel):
    username: str
    password: str


@app.post("/login/")
async def login(data: Annotated[FormData, Form()]):
    return data

When I test it by accessing http://127.0.0.1:8000/docs, the form doesn't appear; instead, it shows a JSON example. enter image description here

If I input data using JSON format, I get the following error: 422 Unprocessable Entity.

What steps am I doing wrong?


Solution

  • As @MatsLindh said, when I upgrade to a newer version, the form will be shown correctly.

    (.venv) PS C:\test\LxFastApi\pythonProject> pip show fastapi     
    Name: fastapi
    Version: 0.114.0
    Summary: FastAPI framework, high performance, easy to learn, fast to code, ready for production
    Home-page: https://github.com/fastapi/fastapi
    Author:
    Author-email: =?utf-8?q?Sebasti=C3=A1n_Ram=C3=ADrez?= <[email protected]>
    License:
    Location: C:\test\LxFastApi\pythonProject\.venv\Lib\site-packages
    Requires: pydantic, starlette, typing-extensions
    Required-by:
    
    
    

    enter image description here