In this post, I am going to explain how to upload a file along with additional form parameters. By defining the files and form fields at the same time using File and Form we can upload the file and additional form parameters.
Example
@app.post("/upload")
def uploadData(files:List[UploadFile] = File(...), msg : str = Form(...))
Find the below working application.
fileAndFormParam.py
from fastapi import FastAPI, File, UploadFile, Form
from typing import List
app = FastAPI()
@app.post("/upload")
def uploadData(files:List[UploadFile] = File(...),
msg : str = Form(...)):
fileNames = []
for file in files:
fileNames.append(file.filename)
return {"fileNames": fileNames, 'msg' : msg}
Open terminal and execute the command ‘uvicorn fileAndFormParam:app --reload’.
$uvicorn fileAndFormParam:app --reload
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [65890] using statreload
INFO: Started server process [65892]
INFO: Waiting for application startup.
INFO: Application startup complete.
Open the url ‘http://localhost:8000/docs’ in browser and experiment with the swagger documentation.
Previous Next Home
No comments:
Post a Comment