正常写法如下:
import typing
from fastapi import FastAPI, File, UploadFile
app = FastAPI(title="Form表单")
'''
上传文件为可选项
'''
@app.post("/upload_large_file", summary="上传大文件")
# 文件可选
# def upload_large_file(file: UploadFile = File(default=None)):
def upload_large_file(file: typing.Optional[UploadFile] = None):
if not file:
return {"data":"no file"}
else:
return file.filename
如果和上面写法一样还是报错,说明是swagger的问题,你不上传文件时它会默认打钩send empty value,取消打钩即可正确请求。