关于python:fastapi-上传文件的同时提交表单

10次阅读

共计 394 个字符,预计需要花费 1 分钟才能阅读完成。

multipart/form-data:须要在表单中进行文件上传时,就须要应用该格局

正确的做法:

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(...), name: str = Form(...)
):
    return {"file_size": len(file),
        "token": token,
        "fileb_content_type": fileb.content_type,
    }

留神:依据 HTTP 协定,既然上传文件又要上传文字,这个文字要是 form,而不能是 json

参考文章:
导入 File 与 Form
HTTP content-type

正文完
 0