Skip to content

Commit 3ab42fc

Browse files
committed
new function: sqlpage.headers()
closes #810
1 parent a6a872e commit 3ab42fc

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
- Control decimal places with `number_format_digits` property.
3636
- Add a new `description_md` row-level property to the form component to allow displaying markdown in a form field description.
3737
- Improve the error message when a header component (e.g. status_code, json, cookie) is used in the wrong place (after data has already been sent to the client).
38+
- New function: [`sqlpage.headers`](https://sql-page.com/functions.sql?function=headers).
3839

3940
## 0.32.1 (2025-01-03)
4041

examples/official-site/sqlpage/migrations/08_functions.sql

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ Log the [`User-Agent`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers
8686
```sql
8787
INSERT INTO user_agent_log (user_agent) VALUES (sqlpage.header(''user-agent''));
8888
```
89+
90+
If you need access to all headers at once, use [`sqlpage.headers()`](?function=headers) instead.
8991
'
9092
);
9193
INSERT INTO sqlpage_function_parameters (
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
INSERT INTO sqlpage_functions (
2+
"name",
3+
"introduced_in_version",
4+
"icon",
5+
"description_md"
6+
)
7+
VALUES (
8+
'headers',
9+
'0.33.0',
10+
'circle-dotted-letter-h',
11+
'Returns all HTTP request headers as a JSON object.
12+
13+
### Example
14+
15+
The following displays all HTTP request headers in a list,
16+
using SQLite''s `json_each()` function.
17+
18+
```sql
19+
select ''list'' as component;
20+
21+
select key as title, value as description
22+
from json_each(sqlpage.headers()); -- json_each() is SQLite only
23+
```
24+
25+
If not on SQLite, use your [database''s JSON function](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide).
26+
27+
### Details
28+
29+
The function returns a JSON object where:
30+
- Keys are lowercase header names
31+
- Values are the corresponding header values
32+
- If no headers are present, returns an empty JSON object `{}`
33+
34+
This is useful when you need to:
35+
- Debug HTTP requests
36+
- Access multiple headers at once
37+
38+
If you only need access to a single known header, use [`sqlpage.header(name)`](?function=header) instead.
39+
');

src/webserver/database/sqlpage_functions/functions.rs

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ super::function_definition_macro::sqlpage_functions! {
2626

2727
hash_password(password: Option<String>);
2828
header((&RequestInfo), name: Cow<str>);
29+
headers((&RequestInfo));
2930

3031
link(file: Cow<str>, parameters: Option<Cow<str>>, hash: Option<Cow<str>>);
3132

@@ -629,3 +630,7 @@ async fn request_body_base64(request: &RequestInfo) -> Option<String> {
629630
);
630631
Some(base64_string)
631632
}
633+
634+
async fn headers(request: &RequestInfo) -> String {
635+
serde_json::to_string(&request.headers).unwrap_or_default()
636+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
select 'text' as component,
2+
case when sqlpage.headers() LIKE '%"cookie":"test_cook=123"%' then 'It works !'
3+
else 'error: ' || sqlpage.headers()
4+
end AS contents;

0 commit comments

Comments
 (0)