FastAPI(25)- File、Form 混合使用

前言

详解 Form 

详解 File

 

路径函数混合使用 Form、File

from fastapi import FastAPI, File, Form, UploadFile

app = FastAPI()


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


if __name__ == "__main__":
    uvicorn.run(app="21_File:app", host="127.0.0.1", port=8080, reload=True, debug=True)

 

正确传参的请求结果

发送表单数据的时候如果有文件,一定要用 form-data !!

 

查看 Swagger API 文档

 

posted @ 2021-09-25 10:16  小菠萝测试笔记  阅读(452)  评论(0编辑  收藏  举报