fastapi 查询参数

参考1

from enum import Enum

from fastapi import APIRouter, Path

app03 = APIRouter()


@app03.get('/path/param')
def path_params01():
    return {'data': 'this s a message'}


@app03.get('/path/{param}')
def path_params01(param: str):
    return {'data': param}


class CityName(str, Enum):
    Beijing = "BJ"
    Shanghai = "SH"


# 枚举用法
@app03.get('/enum/{city}')
async def data_ganran(city: CityName):
    if city == CityName.Shanghai:
        return {'msg': f'{city}感染了100'}
    if city == CityName.Beijing:
        return {'msg': f'{city}感染了200'}
    return {'msg': '---'}


# 通过path传递文件路径
@app03.get('/files/{file_path:path}')
def filepath(file_path: str):
    return f'the file path is {file_path}'


# 校验路径参数
@app03.get('/path/{num}')
def path_param_validate(num: int = Path(..., title='zhe shi title', ge=1, le=6)):
    return num

 

posted @ 2024-06-26 20:26  tslam  阅读(3)  评论(0编辑  收藏  举报