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.url: the URL of the current page, parsed into its components (see URL Attributes below).router.route_id: the route pattern that matched the current request (e.g./posts/[id]). For dynamic pages this contains the slug rather than the actual value used to load the page.router.session: data about the current sessionclient_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.
URL Attributes
self.router.url is the full URL of the page currently displayed in the browser, parsed into its components using Python's standard urllib.parse.urlsplit. It is a string subclass, so it can be used anywhere a string is expected (for example, passed to rx.text(self.router.url) to render the whole URL), and additionally exposes the following attributes:
scheme: The URL scheme (e.g."http"or"https").netloc: The network location, including hostname and optional port (e.g."example.com:3000").origin: The scheme and netloc joined together (e.g."https://example.com:3000"). Equivalent tof"{scheme}://{netloc}".path: The URL path as displayed in the browser, including any filled-in dynamic segments but excluding the query string and fragment (e.g."/posts/123").query: The raw query string, without the leading?(e.g."tab=comments&sort=new").query_parameters: The query string parsed into a frozen, immutableMapping[str, str]. Use this instead of parsingqueryby hand.fragment: The URL fragment, without the leading#(e.g."section-2"). The client-side router sends the current fragment over the WebSocket, so this reflects whatever is shown in the browser URL bar.
Example
For a request to https://example.com:3000/posts/123?tab=comments#top matching the route /posts/[id]:
Reading Query Parameters
query_parameters is the preferred way to read values from the query string. It is a frozen mapping (immutable and hashable), so it is safe to use inside @rx.var computed vars and event handlers:
For dynamic path segments such as [id] or [[...splat]], see Dynamic Routes — those values are exposed as state vars on the root state (e.g. rx.State.id, rx.State.splat), not through router.url.
Migrating from router.page
The self.router.page namespace is deprecated as of Reflex 0.8.1 and will be removed in 1.0. Its functionality is now provided by self.router.url together with self.router.route_id. Use the table below to update existing code:
Example Values on this Page
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.