Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

436
Views
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)
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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())
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!