<h3 dir="auto">First Check</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I added a very descriptive title to this issue.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I used the GitHub search to find a similar issue and didn't find it.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I searched the FastAPI documentation, with the integrated search.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already searched in Google "How to X in FastAPI" and didn't find any information.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already read and followed all the tutorial in the docs and didn't find an answer.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already checked if it is not related to FastAPI but to <a href="https://github.com/samuelcolvin/pydantic">Pydantic</a>.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already checked if it is not related to FastAPI but to <a href="https://github.com/swagger-api/swagger-ui">Swagger UI</a>.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already checked if it is not related to FastAPI but to <a href="https://github.com/Redocly/redoc">ReDoc</a>.</li>
</ul>
<h3 dir="auto">Commit to Help</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I commit to help with one of those options 👆</li>
</ul>
<h3 dir="auto">Example Code</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="@app.post( '/test-fail')
def test( param1: str = Form(...),	# It fails if the File goes first
          file: bytes = File(...)
        ):
    return Response(content=file, media_type=&quot;application/octet-stream&quot;)

@app.post( '/test-success')
def test( file: bytes = File(...),	# It succeeds if the File goes first 
          param1: str = Form(...)
        ):
    return Response(content=file, media_type=&quot;application/octet-stream&quot;)"><pre class="notranslate"><span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">post</span>( <span class="pl-s">'/test-fail'</span>)</span>
<span class="pl-k">def</span> <span class="pl-en">test</span>( <span class="pl-s1">param1</span>: <span class="pl-s1">str</span> <span class="pl-c1">=</span> <span class="pl-v">Form</span>(...),	<span class="pl-c"># It fails if the File goes first</span>
          <span class="pl-s1">file</span>: <span class="pl-s1">bytes</span> <span class="pl-c1">=</span> <span class="pl-v">File</span>(...)
        ):
    <span class="pl-k">return</span> <span class="pl-v">Response</span>(<span class="pl-s1">content</span><span class="pl-c1">=</span><span class="pl-s1">file</span>, <span class="pl-s1">media_type</span><span class="pl-c1">=</span><span class="pl-s">"application/octet-stream"</span>)

<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">post</span>( <span class="pl-s">'/test-success'</span>)</span>
<span class="pl-k">def</span> <span class="pl-en">test</span>( <span class="pl-s1">file</span>: <span class="pl-s1">bytes</span> <span class="pl-c1">=</span> <span class="pl-v">File</span>(...),	<span class="pl-c"># It succeeds if the File goes first </span>
          <span class="pl-s1">param1</span>: <span class="pl-s1">str</span> <span class="pl-c1">=</span> <span class="pl-v">Form</span>(...)
        ):
    <span class="pl-k">return</span> <span class="pl-v">Response</span>(<span class="pl-s1">content</span><span class="pl-c1">=</span><span class="pl-s1">file</span>, <span class="pl-s1">media_type</span><span class="pl-c1">=</span><span class="pl-s">"application/octet-stream"</span>)</pre></div>
<h3 dir="auto">Description</h3>
<p dir="auto"><strong>BUG</strong><br>
The <code class="notranslate">File</code> parameter on the POST method must go first, before other <code class="notranslate">Form</code> parameters. Otherwise the end-point returns HTTP code 422.</p>
<p dir="auto"><strong>Details</strong><br>
A command line like below for the end-points from the example</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="curl --location --request POST 'http://127.0.0.1:5001/test' --form 'param1=&quot;fff&quot;' --form 'file=@&quot;file.png&quot;'"><pre class="notranslate"><code class="notranslate">curl --location --request POST 'http://127.0.0.1:5001/test' --form 'param1="fff"' --form 'file=@"file.png"'
</code></pre></div>
<p dir="auto">would return <em>HTTP code 200</em> with an expected result for <code class="notranslate">/test-success</code> and <em>HTTP code 422</em> for <code class="notranslate">/test-fail</code> with the following JSON:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{&quot;detail&quot;:[{&quot;loc&quot;:[&quot;body&quot;,&quot;file&quot;],&quot;msg&quot;:&quot;byte type expected&quot;,&quot;type&quot;:&quot;type_error.bytes&quot;}]}"><pre class="notranslate"><code class="notranslate">{"detail":[{"loc":["body","file"],"msg":"byte type expected","type":"type_error.bytes"}]}
</code></pre></div>
<p dir="auto"><strong>Expected behaviour</strong><br>
Validation of parameters doesn't depend on the order of the parameters</p>
<h3 dir="auto">Operating System</h3>
<p dir="auto">macOS</p>
<h3 dir="auto">Operating System Details</h3>
<p dir="auto">v12.0.1</p>
<h3 dir="auto">FastAPI Version</h3>
<p dir="auto">0.70.1</p>
<h3 dir="auto">Python Version</h3>
<p dir="auto">3.7.10</p>
<h3 dir="auto">Additional Context</h3>
<p dir="auto"><em>No response</em></p>
<p dir="auto">Here's a <a href="https://github.com/thomasleveil/fastapi/tree/file_and_form_together">branch</a> with my use case:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from starlette.testclient import TestClient

from fastapi import FastAPI, File, Form

app = FastAPI()


@app.post(&quot;/file_before_form&quot;)
def file_before_form(
    file: bytes = File(...), city: str = Form(...),
):
    return {&quot;file_content&quot;: file, &quot;city&quot;: city}


@app.post(&quot;/file_after_form&quot;)
def file_after_form(
    city: str = Form(...), file: bytes = File(...),
):
    return {&quot;file_content&quot;: file, &quot;city&quot;: city}


client = TestClient(app)


def test_file_before_form():
    response = client.post(
        &quot;/file_before_form&quot;, data={&quot;city&quot;: &quot;Thimphou&quot;}, files={&quot;file&quot;: &quot;&lt;file content&gt;&quot;}
    )
    assert response.status_code == 200, response.text
    assert response.json() == {&quot;file_content&quot;: &quot;&lt;file content&gt;&quot;, &quot;city&quot;: &quot;Thimphou&quot;}


def test_file_after_form():
    response = client.post(
        &quot;/file_after_form&quot;, data={&quot;city&quot;: &quot;Thimphou&quot;}, files={&quot;file&quot;: &quot;&lt;file content&gt;&quot;}
    )
    assert response.status_code == 200, response.text  # &lt;-------------- this fails since ab2b86f
    assert response.json() == {&quot;file_content&quot;: &quot;&lt;file content&gt;&quot;, &quot;city&quot;: &quot;Thimphou&quot;}
"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">starlette</span>.<span class="pl-s1">testclient</span> <span class="pl-k">import</span> <span class="pl-v">TestClient</span>

<span class="pl-k">from</span> <span class="pl-s1">fastapi</span> <span class="pl-k">import</span> <span class="pl-v">FastAPI</span>, <span class="pl-v">File</span>, <span class="pl-v">Form</span>

<span class="pl-s1">app</span> <span class="pl-c1">=</span> <span class="pl-v">FastAPI</span>()


<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">post</span>(<span class="pl-s">"/file_before_form"</span>)</span>
<span class="pl-k">def</span> <span class="pl-en">file_before_form</span>(
    <span class="pl-s1">file</span>: <span class="pl-s1">bytes</span> <span class="pl-c1">=</span> <span class="pl-v">File</span>(...), <span class="pl-s1">city</span>: <span class="pl-s1">str</span> <span class="pl-c1">=</span> <span class="pl-v">Form</span>(...),
):
    <span class="pl-k">return</span> {<span class="pl-s">"file_content"</span>: <span class="pl-s1">file</span>, <span class="pl-s">"city"</span>: <span class="pl-s1">city</span>}


<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">post</span>(<span class="pl-s">"/file_after_form"</span>)</span>
<span class="pl-k">def</span> <span class="pl-en">file_after_form</span>(
    <span class="pl-s1">city</span>: <span class="pl-s1">str</span> <span class="pl-c1">=</span> <span class="pl-v">Form</span>(...), <span class="pl-s1">file</span>: <span class="pl-s1">bytes</span> <span class="pl-c1">=</span> <span class="pl-v">File</span>(...),
):
    <span class="pl-k">return</span> {<span class="pl-s">"file_content"</span>: <span class="pl-s1">file</span>, <span class="pl-s">"city"</span>: <span class="pl-s1">city</span>}


<span class="pl-s1">client</span> <span class="pl-c1">=</span> <span class="pl-v">TestClient</span>(<span class="pl-s1">app</span>)


<span class="pl-k">def</span> <span class="pl-en">test_file_before_form</span>():
    <span class="pl-s1">response</span> <span class="pl-c1">=</span> <span class="pl-s1">client</span>.<span class="pl-en">post</span>(
        <span class="pl-s">"/file_before_form"</span>, <span class="pl-s1">data</span><span class="pl-c1">=</span>{<span class="pl-s">"city"</span>: <span class="pl-s">"Thimphou"</span>}, <span class="pl-s1">files</span><span class="pl-c1">=</span>{<span class="pl-s">"file"</span>: <span class="pl-s">"&lt;file content&gt;"</span>}
    )
    <span class="pl-k">assert</span> <span class="pl-s1">response</span>.<span class="pl-s1">status_code</span> <span class="pl-c1">==</span> <span class="pl-c1">200</span>, <span class="pl-s1">response</span>.<span class="pl-s1">text</span>
    <span class="pl-k">assert</span> <span class="pl-s1">response</span>.<span class="pl-en">json</span>() <span class="pl-c1">==</span> {<span class="pl-s">"file_content"</span>: <span class="pl-s">"&lt;file content&gt;"</span>, <span class="pl-s">"city"</span>: <span class="pl-s">"Thimphou"</span>}


<span class="pl-k">def</span> <span class="pl-en">test_file_after_form</span>():
    <span class="pl-s1">response</span> <span class="pl-c1">=</span> <span class="pl-s1">client</span>.<span class="pl-en">post</span>(
        <span class="pl-s">"/file_after_form"</span>, <span class="pl-s1">data</span><span class="pl-c1">=</span>{<span class="pl-s">"city"</span>: <span class="pl-s">"Thimphou"</span>}, <span class="pl-s1">files</span><span class="pl-c1">=</span>{<span class="pl-s">"file"</span>: <span class="pl-s">"&lt;file content&gt;"</span>}
    )
    <span class="pl-k">assert</span> <span class="pl-s1">response</span>.<span class="pl-s1">status_code</span> <span class="pl-c1">==</span> <span class="pl-c1">200</span>, <span class="pl-s1">response</span>.<span class="pl-s1">text</span>  <span class="pl-c"># &lt;-------------- this fails since ab2b86f</span>
    <span class="pl-k">assert</span> <span class="pl-s1">response</span>.<span class="pl-en">json</span>() <span class="pl-c1">==</span> {<span class="pl-s">"file_content"</span>: <span class="pl-s">"&lt;file content&gt;"</span>, <span class="pl-s">"city"</span>: <span class="pl-s">"Thimphou"</span>}</pre></div>
<h3 dir="auto">Description</h3>
<p dir="auto">Upgrading FastApi from 0.42.0 to 0.61.0 on one of my project, I stumbled upon a regression in which FastApi will respond with HTTP Error 422 when ever posting a form-data request to an endpoint where a <code class="notranslate">File</code> is declared <strong>after</strong> a <code class="notranslate">Form</code>.</p>
<p dir="auto">Searching the Internet, I found another user suffering from this issue : <a href="https://stackoverflow.com/questions/61376754/fastapi-files-must-be-loaded-before-form-in-function-parameters" rel="nofollow">https://stackoverflow.com/questions/61376754/fastapi-files-must-be-loaded-before-form-in-function-parameters</a></p>
<p dir="auto">I've created a branch with tests to showcase the issue : <a href="https://github.com/thomasleveil/fastapi/tree/file_and_form_together">https://github.com/thomasleveil/fastapi/tree/file_and_form_together</a></p>
<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>OS: Linux</li>
<li>FastAPI Version 0.44.1 and later</li>
<li>Python version: 3.8.5</li>
</ul>
<h3 dir="auto">Additional context</h3>
<p dir="auto">I've bisected the repo and figured out the regression was introduced in commit <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/tiangolo/fastapi/commit/ab2b86fe2ce8fe15e91aaec179438e24ff7b7ed0/hovercard" href="https://github.com/tiangolo/fastapi/commit/ab2b86fe2ce8fe15e91aaec179438e24ff7b7ed0"><tt>ab2b86f</tt></a></p>