Skip to content

Commit c4492c1

Browse files
committed
'fix(fetch_with_meta) :: return json bodies under the documented key
1 parent 6859b56 commit c4492c1

4 files changed

Lines changed: 22 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- `column` charts now display vertical bars instead of nothing at all.
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.
16+
- `sqlpage.fetch_with_meta` now returns JSON response bodies under the documented `body` key.
1617

1718
## v0.45
1819

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub(super) async fn fetch_with_meta(
6161
decode_response(body_bytes, http_request.response_encoding.as_deref())?;
6262
if is_json {
6363
obj.serialize_entry(
64-
"json_body",
64+
"body",
6565
&serde_json::value::RawValue::from_string(body_str)?,
6666
)?;
6767
} else {

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 '"body":{"hello":"world"}' as expected_contains, $res as actual;

0 commit comments

Comments
 (0)