• 查询参数和字符串校验:Query_Parameters_and_String_Validations


    官方文档地址:https://fastapi.tiangolo.com/zh/tutorial/query-params-str-validations/

    # -*- coding: UTF-8 -*-
    from fastapi import FastAPI, Query
    from typing import List
    
    app = FastAPI()
    #########################################################
    # 限制长度
    @app.get("/items/")
    async def read_items(q: str = Query(..., min_length=3, max_length=50)):    
        #填None就是默认值   填 ...则是必填项 
        results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
        if q:
            results.update({"q": q})
        return results
    #########################################################
    #正则表达式
    @app.get("/items2/")
    async def read_items2(
                            q: str = Query(None, min_length=3, max_length=50, regex="^nice")
                         ):
        results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
        if q:
            results.update({"q": q})
        return results
    #########################################################
    #列表
    @app.get("/items3/")
    async def read_items3(q: List[str] = Query(["foo", "bar"])):
        query_items = {"q": q}
        return query_items
    #########################################################
    #别名参数
    @app.get("/items4/")
    async def read_items4(q: str = Query(None, alias="item-query")):
        results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
        if q:
            results.update({"q": q})
        return results
    #########################################################
    #弃用参数
    @app.get("/items5/")
    async def read_items5(
        q: str = Query(
            None,
            alias="item-query",
            title="Query string",
            description="Query string for the items to search in the database that have a good match",
            min_length=3,
            max_length=50,
            regex="^fixedquery$",
            deprecated=True,
        )
    ):
        results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
        if q:
            results.update({"q": q})
        return results
    #########################################################
    
    
    
    if __name__ == '__main__':
        import uvicorn
        uvicorn.run(app, host="127.0.0.1", port=8000)
    
  • 相关阅读:
    iOS优化内存方法推荐
    Swift和OC,是编译型语言、解释性语言、运行时语言
    redis常用命令
    redis 基本类型
    spring中事务配置
    redis 基本概览
    ThreadLocal 类说明
    spring 中 AOP 功能
    ps抠图简单方法
    nginx配置文件中location说明
  • 原文地址:https://www.cnblogs.com/sanduzxcvbnm/p/15932849.html
Copyright © 2020-2023  润新知