首页 > 其他分享 >[FastAPI-29]用户注册API-File字段需要在 Form之前

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

时间:2023-03-26 18:34:39浏览次数:39  
标签:description Form 用户注册 FastAPI avatar File typing

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 ""
    }

标签:description,Form,用户注册,FastAPI,avatar,File,typing
From: https://www.cnblogs.com/leoshi/p/17259153.html

相关文章