Skip to content

Commit 4c88bd7

Browse files
committed
make the headers field in sqlpage.fetch optional
fixes #805
1 parent 8284067 commit 4c88bd7

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- This allows easily implementing autocomplete for form fields with a large number of possible options.
1818
- In the map component, add support for map pins with a description but no title.
1919
- Improved error messages when a parameter of a sqlpage function is invalid. Error traces used to be truncated, and made it hard to understand the exact cause of the error in some cases. In particular, calls to `sqlpage.fetch` would display an unhelpful error message when the HTTP request definition was invalid.
20+
- Make the `headers` field of the `sqlpage.fetch` function parameter optional. It defaults to sending a User-Agent header containing the SQLPage version.
2021

2122
## 0.32.1 (2025-01-03)
2223

examples/official-site/sqlpage/migrations/40_fetch.sql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ set api_results = sqlpage.fetch($request);
8080
# JSON parameter format
8181
8282
The fetch function accepts either a URL string, or a JSON object with the following parameters:
83+
- `url`: The URL to fetch. Required.
8384
- `method`: The HTTP method to use. Defaults to `GET`.
84-
- `url`: The URL to fetch.
85-
- `headers`: A JSON object with the headers to send.
86-
- `body`: The body of the request. If it is a JSON object, it will be sent as JSON. If it is a string, it will be sent as is.
85+
- `headers`: A JSON object with the headers to send. Defaults to sending a User-Agent header containing the SQLPage version.
86+
- `body`: The body of the request. If it is a JSON object, it will be sent as JSON. If it is a string, it will be sent as is. When omitted, no request body is sent.
8787
- `timeout_ms`: The maximum time to wait for the request, in milliseconds. Defaults to 5000.
88-
- `username`: Username for HTTP Basic Authentication. Introduced in version 0.33.0.
89-
- `password`: Password for HTTP Basic Authentication. Only used if username is provided. Introduced in version 0.33.0.
88+
- `username`: Optional username for HTTP Basic Authentication. Introduced in version 0.33.0.
89+
- `password`: Optional password for HTTP Basic Authentication. Only used if username is provided. Introduced in version 0.33.0.
9090
9191
'
9292
);

src/webserver/database/sqlpage_functions/http_fetch_request.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,39 @@ use super::function_traits::BorrowFromStr;
44
use std::borrow::Cow;
55

66
type HeaderVec<'a> = Vec<(Cow<'a, str>, Cow<'a, str>)>;
7+
8+
fn default_headers<'a>() -> HeaderVec<'a> {
9+
vec![
10+
(Cow::Borrowed("Accept"), Cow::Borrowed("*/*")),
11+
(
12+
Cow::Borrowed("User-Agent"),
13+
Cow::Borrowed(concat!(
14+
"SQLPage/v",
15+
env!("CARGO_PKG_VERSION"),
16+
" (+https://sql-page.com)"
17+
)),
18+
),
19+
]
20+
}
21+
722
#[derive(serde::Deserialize, Debug)]
823
#[serde(expecting = "an http request object, e.g. '{\"url\":\"http://example.com\"}'")]
924
pub(super) struct HttpFetchRequest<'b> {
1025
#[serde(borrow)]
1126
pub url: Cow<'b, str>,
1227
#[serde(borrow)]
1328
pub method: Option<Cow<'b, str>>,
14-
pub timeout_ms: Option<u64>,
15-
#[serde(borrow, deserialize_with = "deserialize_map_to_vec_pairs")]
29+
#[serde(
30+
default = "default_headers",
31+
borrow,
32+
deserialize_with = "deserialize_map_to_vec_pairs"
33+
)]
1634
pub headers: HeaderVec<'b>,
1735
pub username: Option<Cow<'b, str>>,
1836
pub password: Option<Cow<'b, str>>,
1937
#[serde(borrow)]
2038
pub body: Option<Cow<'b, serde_json::value::RawValue>>,
39+
pub timeout_ms: Option<u64>,
2140
}
2241

2342
fn deserialize_map_to_vec_pairs<'de, D: serde::Deserializer<'de>>(
@@ -53,7 +72,7 @@ impl<'a> BorrowFromStr<'a> for HttpFetchRequest<'a> {
5372
HttpFetchRequest {
5473
url: s,
5574
method: None,
56-
headers: Vec::new(),
75+
headers: default_headers(),
5776
username: None,
5877
password: None,
5978
body: None,

0 commit comments

Comments
 (0)