Skip to content

Commit c96ac70

Browse files
committed
fix(fetch_with_meta) :: document json bodies under json_body
1 parent e99957b commit c96ac70

5 files changed

Lines changed: 38 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- `stacked` is now ignored on chart types that cannot stack, instead of displaying an empty chart.
1515
- Screen readers now announce the title of the modal component instead of an unnamed dialog.
1616
- `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.
17+
- `sqlpage.fetch_with_meta` now correctly documents server JSON responses sent under `json_body`, not `body`.
1718

1819
## v0.45
1920

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,

tests/common/mod.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,21 @@ pub fn start_echo_server(shutdown: oneshot::Receiver<()>) -> (JoinHandle<()>, u1
149149
let listener = std::net::TcpListener::bind("localhost:0").unwrap();
150150
let port = listener.local_addr().unwrap().port();
151151
let server = HttpServer::new(|| {
152-
App::new().default_service(fn_service(|mut req: ServiceRequest| async move {
153-
let meta = format_request_line_and_headers(&req);
154-
let body = format_body(&mut req).await;
155-
let resp = build_echo_response(body, meta);
156-
Ok(req.into_response(resp))
157-
}))
152+
App::new()
153+
.route(
154+
"/json",
155+
web::to(|body: web::Bytes| async move {
156+
HttpResponse::Ok()
157+
.insert_header((header::CONTENT_TYPE, "application/json"))
158+
.body(body)
159+
}),
160+
)
161+
.default_service(fn_service(|mut req: ServiceRequest| async move {
162+
let meta = format_request_line_and_headers(&req);
163+
let body = format_body(&mut req).await;
164+
let resp = build_echo_response(body, meta);
165+
Ok(req.into_response(resp))
166+
}))
158167
})
159168
.workers(1)
160169
.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)