# 请求(Request)

Request 实例包含许多有关其参数的有用信息。 如果您想了解更多详细信息,请参考 API文档 (opens new window)

# 请求体(Body)

    # 上下文(Context)

    # 请求上下文(Request context)

    request.ctx 对象是存储请求相关信息的地方。

    这里通常被用来存储服务端通过某些验证后需要临时存储的身份认证信息以及专有变量等内容。更多的具体内容我们将在 中间件 这一章节进行更多的描述,下面是一个简单的例子。

    @app.middleware("request")
    async def run_before_handler(request):
        request.ctx.user = await fetch_user_by_token(request.token)
    @app.route('/hi')
    async def hi_my_name_is(request):
        return text("Hi, my name is {}".format(request.ctx.user.name))
    

    最典型的用法就是将从数据库获取的用户对象存储在 request.ctx 中。所有该中间件之后的其他中间件以及请求期间的处理程序都可以对此进行访问。

    自定义上下文是为了应用程序和拓展插件而保留的,Sanic 本身并不使用它。

    # 连接上下文(Connection context)

    通常情况下,您的应用程序需要向同一个客户端提供多个并发(或连续)的请求。这种情况通常发生在需要查询多个端点来获取数据的渐进式网络应用程序中。

    在 HTTP 协议要求通过 keep alive 请求头来减少频繁连接所造成的时间浪费。

    当多个请求共享一个连接时,Sanic 将提供一个上下文对象来允许这些请求共享状态。

    @app.on_request
    async def increment_foo(request):
        if not hasattr(request.conn_info.ctx, "foo"):
            request.conn_info.ctx.foo = 0
        request.conn_info.ctx.foo += 1
    @app.get("/")
    async def count_foo(request):
        return text(f"request.conn_info.ctx.foo={request.conn_info.ctx.foo}")
    
    $ curl localhost:8000 localhost:8000 localhost:8000
    request.conn_info.ctx.foo=1
    request.conn_info.ctx.foo=2
    request.conn_info.ctx.foo=3
    

    # 路由参数(Parameter)

    从路径提取的路由参数将作为参数(或更具体地作为关键字参数)传递到处理程序中。更多的详细内容我们将在 路由 这一章节进行详细说明

    @app.route('/tag/<tag>')
    async def tag_handler(request, tag):
        return text("Tag - {}".format(tag))
    

    # 请求参数(Arguments)

    request 中,您可以通过两种属性来访问请求参数:

    • request.args
    • request.query_args
    $ curl http://localhost:8000\?key1\=val1\&key2\=val2\&key1\=val3
    
    >>> print(request.args)
    {'key1': ['val1', 'val3'], 'key2': ['val2']}
    >>> print(request.args.get("key1"))
    val1
    >>> print(request.args.getlist("key1"))
    ['val1', 'val3']
    >>> print(request.query_args)
    [('key1', 'val1'), ('key2', 'val2'), ('key1', 'val3')]
    >>> print(request.query_string)
    key1=val1&key2=val2&key1=val3
    

    小提示:

    💡 和上述的​ request.formrequest.files 对象一样,request.args 同样是少数几种字典之一,每个值都是一个列表。这是因为HTTP允许单个键名被重用以发送多个值。

    获取方式也和 它们一致,大多数情况下您只需要使用 .get() 方法来获取列表中的第一个元素即可,如果您想获取列表中的全部元素,那么请使用 .getlist() 方法。

    MIT Licensed
    Copyright © 2018-present Sanic Community Organization

    ~ Made with ❤️ and ☕️ ~