title: How to use a Pydantic model with Form data in FastAPI?
body: <p>I am trying to submit data from HTML forms and validate it with a Pydantic model.</p>
  <p>Using this code</p>
  <pre><code>from fastapi import FastAPI, Form
  from pydantic import BaseModel
  from starlette.responses import HTMLResponse
  
  
  app = FastAPI()
  
  @app.get(&quot;/form&quot;, response_class=HTMLResponse)
  def form_get():
      return '''&lt;form method=&quot;post&quot;&gt; 
      &lt;input type=&quot;text&quot; name=&quot;no&quot; value=&quot;1&quot;/&gt; 
      &lt;input type=&quot;text&quot; name=&quot;nm&quot; value=&quot;abcd&quot;/&gt; 
      &lt;input type=&quot;submit&quot;/&gt; 
      &lt;/form&gt;'''
  
  
  class SimpleModel(BaseModel):
      no: int
      nm: str = &quot;&quot;
  
  @app.post(&quot;/form&quot;, response_model=SimpleModel)
  def form_post(form_data: SimpleModel = Form(...)):
      return form_data
  </code></pre>
  <p>However, I get the HTTP error: &quot;<code>422</code> Unprocessable Entity&quot;</p>
  <pre><code>{
      &quot;detail&quot;: [
          {
              &quot;loc&quot;: [
                  &quot;body&quot;,
                  &quot;form_data&quot;
              ],
              &quot;msg&quot;: &quot;field required&quot;,
              &quot;type&quot;: &quot;value_error.missing&quot;
          }
      ]
  }
  </code></pre>
  <p>The equivalent curl command (generated by Firefox) is</p>
  <pre class="lang-none prettyprint-override"><code>curl 'http://localhost:8001/form' -H 'Content-Type: application/x-www-form-urlencoded' --data 'no=1&amp;nm=abcd'
  </code></pre>
  <p>Here the request body contains <code>no=1&amp;nm=abcd</code>.</p>
  <p>What am I doing wrong?</p>