multipart/form-data
: 须要在表单中进行文件上传时,就须要应用该格局
正确的做法:
from fastapi import FastAPI, File, Form, UploadFileapp = 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