跳至内容

HTTPConnection

当你想定义与 HTTP 和 WebSockets 都兼容的依赖项时,你可以定义一个参数,它接受一个 HTTPConnection 而不是 RequestWebSocket

你可以从 fastapi.requests 中导入它

from fastapi.requests import HTTPConnection

fastapi.requests.HTTPConnection

HTTPConnection(scope, receive=None)

基类:Mapping[str, Any]

传入 HTTP 连接的基类,用于提供 RequestWebSocket 共有的任何功能。

参数 描述
scope

类型: Scope

receive

类型: Receive | None 默认值: None

源代码在 starlette/requests.py
71
72
73
def __init__(self, scope: Scope, receive: Receive | None = None) -> None:
    assert scope["type"] in ("http", "websocket")
    self.scope = scope

scope instance-attribute

scope = scope

app property

app

url property

url

base_url property

base_url

headers property

headers

query_params property

query_params

path_params property

path_params

cookies property

cookies

client 属性

client

session 属性

session

auth 属性

auth

user 属性

user

state 属性

state

url_for

url_for(name, /, **path_params)
参数 描述
名称

类型: str

**路径参数**

类型: 任何类型 默认值: {}

源代码在 starlette/requests.py
177
178
179
180
def url_for(self, name: str, /, **path_params: typing.Any) -> URL:
    router: Router = self.scope["router"]
    url_path = router.url_path_for(name, **path_params)
    return url_path.make_absolute_url(base_url=self.base_url)