Skip to content

HTTPConnection classยถ

When you want to define dependencies that should be compatible with both HTTP and WebSockets, you can define a parameter that takes an HTTPConnection instead of a Request or a WebSocket.

You can import it from fastapi.requests:

from fastapi.requests import HTTPConnection

fastapi.requests.HTTPConnection ยถ

HTTPConnection(scope, receive=None)

Bases: Mapping[str, Any]

A base class for incoming HTTP connections, that is used to provide any functionality that is common to both Request and WebSocket.

PARAMETER DESCRIPTION
scope

TYPE: Scope

receive

TYPE: Optional[Receive] DEFAULT: None

Source code in starlette/requests.py
69
70
71
def __init__(self, scope: Scope, receive: typing.Optional[Receive] = 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 property ยถ

client

session property ยถ

session

auth property ยถ

auth

user property ยถ

user

state property ยถ

state

url_for ยถ

url_for(__name, **path_params)
PARAMETER DESCRIPTION
__name

TYPE: str

**path_params

TYPE: Any DEFAULT: {}

Source code in starlette/requests.py
176
177
178
179
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)