State Utility Methods

The state object has several methods and attributes that return information about the current page, session, or state.

Router Attributes

The self.router attribute has several sub-attributes that provide various information:

  • router.page: data about the current page and route
    • host: The hostname and port serving the current page (frontend).
    • path: The path of the current page (for dynamic pages, this will contain the slug)
    • raw_path: The path of the page displayed in the browser (including params and dynamic values)
    • full_path: path with host prefixed
    • full_raw_path: raw_path with host prefixed
    • params: Dictionary of query params associated with the request
  • router.session: data about the current session
    • client_token: UUID associated with the current tab's token. Each tab has a unique token.
    • session_id: The ID associated with the client's websocket connection. Each tab has a unique session ID.
    • client_ip: The IP address of the client. Many users may share the same IP address.
  • router.headers: headers associated with the websocket connection. These values can only change when the websocket is re-established (for example, during page refresh).
    • host: The hostname and port serving the websocket (backend).
    • origin: The origin of the request.
    • upgrade: The upgrade header for websocket connections.
    • connection: The connection header.
    • cookie: The cookie header.
    • pragma: The pragma header.
    • cache_control: The cache control header.
    • user_agent: The user agent string of the client.
    • sec_websocket_version: The websocket version.
    • sec_websocket_key: The websocket key.
    • sec_websocket_extensions: The websocket extensions.
    • accept_encoding: The accepted encodings.
    • accept_language: The accepted languages.
    • raw_headers: A mapping of all HTTP headers as a frozen dictionary. This provides access to any header that was sent with the request, not just the common ones listed above.

Example Values on this Page

NameValue
rx.State.router.page.host
rx.State.router.page.path
rx.State.router.page.raw_path
rx.State.router.page.full_path
rx.State.router.page.full_raw_path
rx.State.router.page.params{}
rx.State.router.session.client_token
rx.State.router.session.session_id
rx.State.router.session.client_ip
rx.State.router.headers.host
rx.State.router.headers.origin
rx.State.router.headers.upgrade
rx.State.router.headers.connection
rx.State.router.headers.cookie
rx.State.router.headers.pragma
rx.State.router.headers.cache_control
rx.State.router.headers.user_agent
rx.State.router.headers.sec_websocket_version
rx.State.router.headers.sec_websocket_key
rx.State.router.headers.sec_websocket_extensions
rx.State.router.headers.accept_encoding
rx.State.router.headers.accept_language
rx.State.router.headers.raw_headers{}

Accessing Raw Headers

The raw_headers attribute provides access to all HTTP headers as a frozen dictionary. This is useful when you need to access headers that are not explicitly defined in the HeaderData class:

This is particularly useful for accessing custom headers or when working with specific HTTP headers that are not part of the standard set exposed as direct attributes.

Built with Reflex