I am trying to submit data from html forms and on the validate it with pydantic model.
Using this code
from fastapi import FastAPI, Form
from pydantic import BaseModel
from starlette.responses import HTMLResponse
app = FastAPI()
@app.get("/form", response_class=HTMLResponse)
def form_get():
return '''<form method="post">
<input type="text" name="no" value="1"/>
<input type="text" name="nm" value="abcd"/>
<input type="submit"/>
</form>'''
class SimpleModel(BaseModel):
no: int
nm: str = ""
@app.post("/form", response_model=SimpleModel)
def form_post(form_data: SimpleModel = Form(...)):
return form_data
How ever I get the error with http status 422 Unprocessable Entity
{"detail":[{"loc":["body","form_data"],"msg":"field required","type":"value_error.missing"}]}
Equivalent curl command (generated by firfox) is
curl 'http://localhost:8001/form' -H 'Content-Type: application/x-www-form-urlencoded' --data 'no=1&nm=abcd'
Here the request body contains no=1&nm=abcd
What am i doing wrong?
form_datais missing. But impossible to help more without seeing what you're submitting. - SColvinno=1&nm=abcd- shanmuga