[FastAPI-29]用户注册API-File字段需要在 Form之前

发布时间 2023-03-26 18:28:49作者: LeoShi2020
import typing
from fastapi import FastAPI, Form, File, UploadFile
from pydantic import BaseModel

app = FastAPI(title="注册接口")

'''
1. 需要输入 账号 密码 头像 
'''

@app.post("/register")
##### File字段需要在 Form之前
def register(
    avatar: typing.Optional[bytes] = File(description="头像", default=None),
    username: str = Form(description="用户名"),
    password: str = Form(description="密码:6-10位",
                         min_length=6,
                         max_length=10),
    accessary: typing.Optional[UploadFile] = File(description="大的附件", default=None)
):
    return {
        "username":username,
        "password":password,
        "avatar":len(avatar) if avatar else 0,
        "accessory":accessary.filename if accessary else ""
    }