title: How to Upload File using FastAPI?
body: <p>I am using FastAPI to upload a file according to the official documentation, as shown below:</p>
  <pre><code>@app.post(&quot;/create_file/&quot;)
  async def create_file(file: UploadFile = File(...)):
        file2store = await file.read()
        # some code to store the BytesIO(file2store) to the other database
  </code></pre>
  <p>When I send a request using Python requests library, as shown below:</p>
  <pre><code>f = open(&quot;.../file.txt&quot;, 'rb')
  files = {&quot;file&quot;: (f.name, f, &quot;multipart/form-data&quot;)}
  requests.post(url=&quot;SERVER_URL/create_file&quot;, files=files)
  </code></pre>
  <p>the <code>file2store</code> variable is always empty. Sometimes (rarely seen), it can get the file bytes, but almost all the time it is empty, so I can't restore the file on the other database.</p>
  <p>I also tried the <code>bytes</code> rather than <code>UploadFile</code>, but I get the same results. Is there something wrong in my code, or is the way I use FastAPI to upload a file wrong?</p>