FastAPI支持同时使用File和Form定义文件和表单字段。

from fastapi import FastAPI, File, Form, UploadFile

app = FastAPI()

@app.post("/files/")
async def create_file(
        file: bytes = File(...), fileb: UploadFile = File(...), token: str = Form(...)
):
    return {
        "file_size": len(file),
        "token": token,
        "fileb_content_type": fileb.content_type
    }

文件和表单字段以表单数据的形式上传,这样就能接收文件与表单字段了。

声明文件可以使用 bytes 或 UploadFile 。

posted on 2022-05-05 19:19  司徒轩宇  阅读(154)  评论(0)    收藏  举报