• FastAPI学习3.get 请求 query params 查询参数 上海


    前言

    get 请求的参数在url 后面带着,一般叫query params 查询参数

    查询参数

    声明不属于路径参数的其他函数参数时,它们将被自动解释为"查询字符串"参数

    from fastapi import FastAPI
    
    app = FastAPI()
    
    fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]
    
    
    @app.get("/items/")
    async def read_item(skip: int = 0, limit: int = 10):
        return fake_items_db[skip : skip + limit]
    

    查询字符串是键值对的集合,这些键值对位于 URL 的 之后,并以 & 符号分隔。

    例如,在以下 url 中:

    http://127.0.0.1:8000/items/?skip=0&limit=10
    

    查询参数skip:对应的值为 0, limit:对应的值为 10
    由于它们是 URL 的一部分,因此它们的"原始值"是字符串。
    但是,当你为它们声明了 Python 类型(在上面的示例中为 int)时,它们将转换为该类型并针对该类型进行校验。
    应用于路径参数的所有相同过程也适用于查询参数:

    • (很明显的)编辑器支持
    • 数据"解析"
    • 数据校验
    • 自动生成文档

    默认值

    由于查询参数不是路径的固定部分,因此它们可以是可选的,并且可以有默认值。
    在上面的示例中,它们具有 skip=0 和 limit=10 的默认值。
    因此,访问 URL:http://127.0.0.1:8000/items/
    将与访问以下地址相同:

    http://127.0.0.1:8000/items/?skip=0&limit=10
    

    但是,如果你访问的是:http://127.0.0.1:8000/items/?skip=20
    函数中的参数值将会是:

    • skip=20:在 URL 中设定的值
    • limit=10:使用默认值

    可选参数

    通过同样的方式,你可以将它们的默认值设置为 None 来声明可选查询参数:

    from typing import Optional
    
    from fastapi import FastAPI
    
    app = FastAPI()
    
    
    @app.get("/items/{item_id}")
    async def read_item(item_id: str, q: Optional[str] = None):
        if q:
            return {"item_id": item_id, "q": q}
        return {"item_id": item_id}
    

    在这个例子中,函数参数 q 将是可选的,并且默认值为 None。

    查询参数类型转换

    你还可以声明 bool 类型,它们将被自动转换:

    from typing import Optional
    
    from fastapi import FastAPI
    
    app = FastAPI()
    
    
    @app.get("/items/{item_id}")
    async def read_item(item_id: str, q: Optional[str] = None, short: bool = False):
        item = {"item_id": item_id}
        if q:
            item.update({"q": q})
        if not short:
            item.update(
                {"description": "This is an amazing item that has a long description"}
            )
        return item
    

    这个例子中,如果你访问:http://127.0.0.1:8000/items/foo?short=1 或者 short=True, short=true, short=on, short=yes
    或任何其他的变体形式(大写,首字母大写等等),你的函数接收的 short 参数都会是布尔值 True。对于值为 False 的情况也是一样的。

    多个路径和查询参数

    你可以同时声明多个路径参数和查询参数,FastAPI 能够识别它们。
    而且你不需要以任何特定的顺序来声明。它们将通过名称被检测到:

    from typing import Optional
    
    from fastapi import FastAPI
    
    app = FastAPI()
    
    
    @app.get("/users/{user_id}/items/{item_id}")
    async def read_user_item(
        user_id: int, item_id: str, q: Optional[str] = None, short: bool = False
    ):
        item = {"item_id": item_id, "owner_id": user_id}
        if q:
            item.update({"q": q})
        if not short:
            item.update(
                {"description": "This is an amazing item that has a long description"}
            )
        return item
    

    必填项查询参数

    当你为非路径参数声明了默认值时(目前而言,我们所知道的仅有查询参数),则该参数不是必需的。
    如果你不想添加一个特定的值,而只是想使该参数成为可选的,则将默认值设置为 None。
    但当你想让一个查询参数成为必需的,不声明任何默认值就可以:

    from fastapi import FastAPI
    
    app = FastAPI()
    
    
    @app.get("/items/{item_id}")
    async def read_user_item(item_id: str, needy: str):
        item = {"item_id": item_id, "needy": needy}
        return item
    

    这里的查询参数 needy 是类型为 str 的必需查询参数。
    如果你在浏览器中打开一个像下面的 URL:http://127.0.0.1:8000/items/foo-item
    因为没有添加必需的参数 needy,你将看到类似以下的错误:

    {
        "detail": [
            {
                "loc": [
                    "query",
                    "needy"
                ],
                "msg": "field required",
                "type": "value_error.missing"
            }
        ]
    }
    

    由于 needy 是必需参数,因此你需要在 URL 中设置它的值:http://127.0.0.1:8000/items/foo-item?needy=sooooneedy

    {
        "item_id": "foo-item",
        "needy": "sooooneedy"
    }
    

    当然,你也可以定义一些参数为必需的,一些具有默认值,而某些则完全是可选的:

    from typing import Optional
    
    from fastapi import FastAPI
    
    app = FastAPI()
    
    
    @app.get("/items/{item_id}")
    async def read_user_item(
        item_id: str, needy: str, skip: int = 0, limit: Optional[int] = None
    ):
        item = {"item_id": item_id, "needy": needy, "skip": skip, "limit": limit}
        return item
    

    在这个例子中,有3个查询参数:

    • needy,一个必需的 str 类型参数。
    • skip,一个默认值为 0 的 int 类型参数。
    • limit,一个可选的 int 类型参数。
  • 相关阅读:
    《大型网站技术架构》学习笔记——架构演化
    ASP.NET MVC之Html.RenderAction
    设计模式学习笔记——面向对象设计原则
    设计模式学习笔记——类图
    观察者模式
    泛型接口
    泛型的实例化应用
    多线程第二种方式-实现runnable
    对象中的某个属性不希望被序例化--transient
    对象序例化
  • 原文地址:https://www.cnblogs.com/yoyoketang/p/15952841.html
Copyright © 2020-2023  润新知