'FastAPI - How to upload file via form

I am making a REST API that requires uploading files. For testing purposes, I am uploading files through postman, but I don't know how to access the files on server side. I was able to retrieve the _dict for authentication purposes, but for the file, it returns None.

#arguments - create, delete, view, list, search, upload, download
@app.post("/incident-resource/{service}")
async def incident_resource_service_handler(service, request: Request):
    try:
        session = Session()
        reqDataForm: FormData = await request.form()
        reqData = reqDataForm._dict 

        file = reqDataForm.get('resource')  # in form(key='resource', value=abc.jpeg)
        print(type(file))                   #< class 'NoneType' >

        user = usr_getAuthenticatedUser(session, reqData)
        userRole = getRole(session, user.role_id)
    except Exception as e:
        session.rollback()
        return handleException(e)


Solution 1:[1]

As per the documentation, "files will be uploaded as "form data", and you receive them by either declaring the type in your endpoint as bytes or UploadFile. If you need to send additional data along with the file, you may have a look at this answer.

If, however, you need to do that in the way you approach the problem in your question, have a look below. As per Starlette's documentation, you can get the file name and the contents as below. Whatever key name you give on the client side, you have to use that one on the server side as well. Below "upload_file" is used as the key. If multiple, or large size files need to be received and saved, consider using async writing instead, as described here.

server side

@app.post("/upload")
async def create_file(request: Request):
    form = await request.form()
    filename = form["upload_file"].filename
    contents = await form["upload_file"].read()
    with open(filename, 'wb') as f:
        f.write(contents)
    return filename

client side (example using Python requests)

import requests

url = 'http://127.0.0.1:8000/upload'
file = {'upload_file': open('test_files/a.txt','rb')}
resp = requests.post(url=url, files = file) 
print(resp.json())

client side (example using FastAPI's TestClient)

from fastapi.testclient import TestClient
from app import app

client = TestClient(app)
file = {'upload_file': open('test_files/a.txt','rb')}
response = client.post("/upload", files = file)
print(response.json())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1