Skip to content

Commit 0639548

Browse files
Update router attributes documentation with raw_headers (#1328)
* Update router attributes documentation with raw_headers Co-Authored-By: Alek Petuskey <[email protected]> * Fix code block format and reset whitelist Co-Authored-By: Alek Petuskey <[email protected]> * Update all Python code blocks to use box mode Co-Authored-By: Alek Petuskey <[email protected]> * Revert whitelist.py to original implementation Co-Authored-By: Alek Petuskey <[email protected]> * Update raw_headers to use to_string() instead of str() Co-Authored-By: Alek Petuskey <[email protected]> * Address PR comments: restore comment in whitelist.py and remove box from eval code block Co-Authored-By: Alek Petuskey <[email protected]> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Alek Petuskey <[email protected]> Co-authored-by: Alek Petuskey <[email protected]>
1 parent ed4ec10 commit 0639548

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

docs/utility_methods/router_attributes.md

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
```python exec
1+
```python exec box
22
import reflex as rx
33
from pcweb.styles.styles import get_code_style, cell_style
44

@@ -17,8 +17,19 @@ router_data = [
1717
{"name": "rx.State.router.session.session_id", "value": RouterState.router.session.session_id},
1818
{"name": "rx.State.router.session.client_ip", "value": RouterState.router.session.client_ip},
1919
{"name": "rx.State.router.headers.host", "value": RouterState.router.headers.host},
20+
{"name": "rx.State.router.headers.origin", "value": RouterState.router.headers.origin},
21+
{"name": "rx.State.router.headers.upgrade", "value": RouterState.router.headers.upgrade},
22+
{"name": "rx.State.router.headers.connection", "value": RouterState.router.headers.connection},
23+
{"name": "rx.State.router.headers.cookie", "value": RouterState.router.headers.cookie},
24+
{"name": "rx.State.router.headers.pragma", "value": RouterState.router.headers.pragma},
25+
{"name": "rx.State.router.headers.cache_control", "value": RouterState.router.headers.cache_control},
2026
{"name": "rx.State.router.headers.user_agent", "value": RouterState.router.headers.user_agent},
21-
{"name": "rx.State.router.headers.to_string()", "value": RouterState.router.headers.to_string()},
27+
{"name": "rx.State.router.headers.sec_websocket_version", "value": RouterState.router.headers.sec_websocket_version},
28+
{"name": "rx.State.router.headers.sec_websocket_key", "value": RouterState.router.headers.sec_websocket_key},
29+
{"name": "rx.State.router.headers.sec_websocket_extensions", "value": RouterState.router.headers.sec_websocket_extensions},
30+
{"name": "rx.State.router.headers.accept_encoding", "value": RouterState.router.headers.accept_encoding},
31+
{"name": "rx.State.router.headers.accept_language", "value": RouterState.router.headers.accept_language},
32+
{"name": "rx.State.router.headers.raw_headers", "value": RouterState.router.headers.raw_headers.to_string()},
2233
]
2334

2435
```
@@ -45,11 +56,21 @@ The `self.router` attribute has several sub-attributes that provide various info
4556
* `session_id`: The ID associated with the client's websocket connection. Each tab has a unique session ID.
4657
* `client_ip`: The IP address of the client. Many users may share the same IP address.
4758

48-
* `router.headers`: a selection of common headers associated with the websocket
49-
connection. These values can only change when the websocket is re-established
50-
(for example, during page refresh). All other headers are available in the
51-
dictionary `self.router_data.headers`.
59+
* `router.headers`: headers associated with the websocket connection. These values can only change when the websocket is re-established (for example, during page refresh).
5260
* `host`: The hostname and port serving the websocket (backend).
61+
* `origin`: The origin of the request.
62+
* `upgrade`: The upgrade header for websocket connections.
63+
* `connection`: The connection header.
64+
* `cookie`: The cookie header.
65+
* `pragma`: The pragma header.
66+
* `cache_control`: The cache control header.
67+
* `user_agent`: The user agent string of the client.
68+
* `sec_websocket_version`: The websocket version.
69+
* `sec_websocket_key`: The websocket key.
70+
* `sec_websocket_extensions`: The websocket extensions.
71+
* `accept_encoding`: The accepted encodings.
72+
* `accept_language`: The accepted languages.
73+
* `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.
5374

5475
### Example Values on this Page
5576

@@ -74,3 +95,22 @@ rx.table.root(
7495
margin_y="1em",
7596
)
7697
```
98+
99+
### Accessing Raw Headers
100+
101+
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:
102+
103+
```python box
104+
# Access a specific header
105+
custom_header_value = self.router.headers.raw_headers.get("x-custom-header", "")
106+
107+
# Example of accessing common headers
108+
user_agent = self.router.headers.raw_headers.get("user-agent", "")
109+
content_type = self.router.headers.raw_headers.get("content-type", "")
110+
authorization = self.router.headers.raw_headers.get("authorization", "")
111+
112+
# You can also check if a header exists
113+
has_custom_header = "x-custom-header" in self.router.headers.raw_headers
114+
```
115+
116+
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.

0 commit comments

Comments
 (0)