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:

  1. from fastapi.requests import HTTPConnection

fastapi.requests.HTTPConnection

  1. 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.

PARAMETERDESCRIPTION
scope

TYPE: Scope

receive

TYPE: Optional[Receive] DEFAULT: None

Source code in starlette/requests.py

  1. 69
  2. 70
  3. 71
  1. def init(self, scope: Scope, receive: typing.Optional[Receive] = None) -> None:
  2. assert scope[type] in (http, websocket)
  3. self.scope = scope

scope instance-attribute

  1. scope = scope

app property

  1. app

url property

  1. url

base_url property

  1. base_url

headers property

  1. headers

query_params property

  1. query_params

path_params property

  1. path_params

cookies property

  1. cookies

client property

  1. client

session property

  1. session

auth property

  1. auth

user property

  1. user

state property

  1. state

url_for

  1. url_for(__name, **path_params)
PARAMETERDESCRIPTION
__name

TYPE: str

**path_params

TYPE: Any DEFAULT: {}

Source code in starlette/requests.py

  1. 176
  2. 177
  3. 178
  4. 179
  1. def url_for(self, name: str, **path_params: typing.Any) -> URL:
  2. router: Router = self.scope[router]
  3. url_path = router.url_path_for(name, **path_params)
  4. return url_path.make_absolute_url(base_url=self.base_url)