Skip to content

Commit f7ebba6

Browse files
authored
fix(fetch_with_meta) :: document json bodies under json_body (#1393)
1 parent bca732e commit f7ebba6

6 files changed

Lines changed: 39 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- Screen readers now announce the title of the modal component instead of an unnamed dialog.
2121
- `sqlpage.request_body` and `sqlpage.request_body_base64` now return NULL when the request has no body. A body that cannot be read, such as one exceeding the payload limit, is now reported as an error instead of being silently replaced with an empty body.
2222
- List-valued configuration options, including OIDC paths and trusted audiences, can now be set through environment variables as space-separated lists.
23+
- `sqlpage.fetch_with_meta` now correctly documents server JSON responses sent under `json_body`, not `body`.
2324
- Datagrid rows with an icon or image no longer display an unnecessary en-dash placeholder, and an explicitly empty description remains empty.
2425
- Charts can display reference lines. A row with a `yline` is drawn as a line across the chart at that value of the y axis, with the row's `label` and `color` for its text and its color. Reference lines are rows, so a chart can have as many of them as the query returns. A line follows its axis, so on a `horizontal` bar chart a `yline` is drawn down the chart rather than across it. They are not added to the total of a `stacked` chart, and are not filled in an `area` chart.
2526

examples/official-site/sqlpage/migrations/58_fetch_with_meta.sql

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,27 @@ VALUES (
1111
'Sends an HTTP request and returns detailed metadata about the response, including status code, headers, and body.
1212
1313
This function is similar to [`fetch`](?function=fetch), but returns a JSON object containing detailed information about the response.
14-
The returned object has the following structure:
14+
When the response declares a `content-type` of `application/json`, the parsed body is returned under `json_body`:
15+
```json
16+
{
17+
"status": 200,
18+
"headers": {
19+
"content-type": "application/json",
20+
"content-length": "1234"
21+
},
22+
"json_body": { "name": "ditto" }
23+
}
24+
```
25+
26+
For every other content type, the body is returned as a string under `body`:
1527
```json
1628
{
1729
"status": 200,
1830
"headers": {
1931
"content-type": "text/html",
2032
"content-length": "1234"
2133
},
22-
"body": "a string, or a json object, depending on the content type",
23-
"error": "error message if any"
34+
"body": "<html>...</html>"
2435
}
2536
```
2637
@@ -42,8 +53,8 @@ where
4253
-- Extract data from the response json body
4354
select ''card'' as component;
4455
select
45-
json_extract($response, ''$.body.name'') as title,
46-
json_extract($response, ''$.body.abilities[0].ability.name'') as description
56+
json_extract($response, ''$.json_body.name'') as title,
57+
json_extract($response, ''$.json_body.abilities[0].ability.name'') as description
4758
from $response;
4859
```
4960

examples/official-site/your-first-sql-website/index.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ SET req = '{
2323
"timeout_ms": 200
2424
}';
2525
SET api_results = sqlpage.fetch_with_meta($req);
26-
SET sqlpage_version = COALESCE(json_extract($api_results, '$.body.tag_name'), '');
26+
SET sqlpage_version = COALESCE(json_extract($api_results, '$.json_body.tag_name'), '');
2727

2828
SELECT 'hero' as component,
2929
'Your first SQL Website' as title,

src/webserver/database/sqlpage_functions/functions/user_info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub(super) async fn user_info<'a>(
6464
"gender" => claims.gender().map(|g| g.to_string()), // Assumes GenderClaim impls ToString
6565
"birthdate" => claims.birthdate().map(|b| b.to_string()), // Assumes Birthdate impls ToString
6666
"zoneinfo" => claims.zoneinfo().map(|z| z.to_string()), // Assumes ZoneInfo impls ToString
67-
"locale" => claims.locale().map(ToString::to_string), // Assumes Locale impls ToString
67+
"locale" => claims.locale().map(ToString::to_string), // Assumes Locale impls ToString
6868
"updated_at" => claims.updated_at().map(|t| t.timestamp().to_string()),
6969

7070
// Standard Claims (Email Scope)

tests/common/mod.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,21 @@ pub(crate) fn start_echo_server(shutdown: oneshot::Receiver<()>) -> (JoinHandle<
150150
let listener = std::net::TcpListener::bind("localhost:0").unwrap();
151151
let port = listener.local_addr().unwrap().port();
152152
let server = HttpServer::new(|| {
153-
App::new().default_service(fn_service(|mut req: ServiceRequest| async move {
154-
let meta = format_request_line_and_headers(&req);
155-
let body = format_body(&mut req).await;
156-
let resp = build_echo_response(&body, meta);
157-
Ok(req.into_response(resp))
158-
}))
153+
App::new()
154+
.route(
155+
"/json",
156+
web::to(|body: web::Bytes| async move {
157+
HttpResponse::Ok()
158+
.insert_header((header::CONTENT_TYPE, "application/json"))
159+
.body(body)
160+
}),
161+
)
162+
.default_service(fn_service(|mut req: ServiceRequest| async move {
163+
let meta = format_request_line_and_headers(&req);
164+
let body = format_body(&mut req).await;
165+
let resp = build_echo_response(&body, meta);
166+
Ok(req.into_response(resp))
167+
}))
159168
})
160169
.workers(1)
161170
.listen(listener)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
set url = 'http://localhost:' || $echo_port || '/json';
2+
set fetch_req = '{"method":"POST","url":"' || $url || '","body":{"hello":"world"}}';
3+
set res = sqlpage.fetch_with_meta($fetch_req);
4+
5+
select '"json_body":{"hello":"world"}' as expected_contains, $res as actual;

0 commit comments

Comments
 (0)