fastapi python python-3.x python-requests

Send and receive file using Python: FastAPI and requests

I’m trying to upload a file to a FastAPI server using requests.

I’ve boiled the problem down to its simplest components.

The client using requests:

import requests
files = {'file': ('foo.txt', open('./foo.txt', 'rb'))}
response ='', files=files)

The server using fastapi:

from fastapi import FastAPI, File, UploadFile
import uvicorn
app = FastAPI()'/file')
def _file_upload(my_file: UploadFile = File(...)):
if __name__ == "__main__":"main:app", host="", port=8000, log_level="debug")

Packages installed:

  • fastapi
  • python-multipart
  • uvicorn
  • requests

Client Output:
<Response [422]>
{‘detail’: [{‘loc’: [‘query’, ‘my_file’], ‘msg’: ‘field required’, ‘type’: ‘value_error.missing’}]}

Server Output:
INFO: – “POST /file HTTP/1.1” 422 Unprocessable Entity

What am I missing here?

FastAPI expecting the file in the my_file field and you are sending it to the file field.

it should be as

import requests
url = ""
files = {'my_file': open('', 'rb')}
res =, files=files)

Also, you don’t need a tuple to manage the upload file (we’re dealing with a simple upload, right?)