0
0
FastapiHow-ToBeginner · 3 min read

How to Get Form Data in FastAPI: Simple Guide with Examples

In FastAPI, you get form data by importing and using the Form class from fastapi. Define your endpoint parameters with Form(...) to tell FastAPI to expect form data instead of JSON.
📐

Syntax

To get form data in FastAPI, import Form from fastapi. Then, in your path operation function, declare parameters with Form(...). This tells FastAPI to extract these values from the form body of the request.

  • Form(...): Marks a parameter as coming from form data and makes it required.
  • Parameters can be strings, integers, or other types supported by FastAPI.
python
from fastapi import FastAPI, Form

app = FastAPI()

@app.post("/login")
async def login(username: str = Form(...), password: str = Form(...)):
    return {"username": username, "password": password}
💻

Example

This example shows a FastAPI app with a POST endpoint /login that accepts username and password as form data. When you send a form with these fields, FastAPI extracts them and returns them in JSON.

python
from fastapi import FastAPI, Form
from fastapi.testclient import TestClient

app = FastAPI()

@app.post("/login")
async def login(username: str = Form(...), password: str = Form(...)):
    return {"username": username, "password": password}

client = TestClient(app)

response = client.post("/login", data={"username": "alice", "password": "secret"})
print(response.json())
Output
{"username": "alice", "password": "secret"}
⚠️

Common Pitfalls

Common mistakes when getting form data in FastAPI include:

  • Not using Form(...) in the function parameters, which makes FastAPI expect JSON instead of form data.
  • Using Body or default parameters instead of Form for form fields.
  • Forgetting to set the request method to POST or the content type to application/x-www-form-urlencoded when testing.

Correct usage requires Form(...) for each form field parameter.

python
from fastapi import FastAPI

app = FastAPI()

# Wrong: missing Form, expects JSON
@app.post("/wrong")
async def wrong(username: str, password: str):
    return {"username": username, "password": password}

# Right: use Form to get form data
from fastapi import Form

@app.post("/right")
async def right(username: str = Form(...), password: str = Form(...)):
    return {"username": username, "password": password}
📊

Quick Reference

Remember these tips when working with form data in FastAPI:

  • Use Form(...) to declare form fields in endpoint parameters.
  • Form data is sent with content type application/x-www-form-urlencoded.
  • Use POST method to send form data.
  • FastAPI automatically parses form data into the declared parameters.

Key Takeaways

Use the Form class from fastapi to declare form data parameters.
Each form field must be a function parameter with Form(...) to be extracted.
Form data is sent with POST requests and content type application/x-www-form-urlencoded.
Without Form(...), FastAPI expects JSON, not form data.
Testing form endpoints requires sending data as form, not JSON.