From de89d9caf4b6ffdb425e7815b19d7e1ea9312bfd Mon Sep 17 00:00:00 2001 From: Ophir Lojkine Date: Sat, 28 Nov 2026 23:46:30 +0000 Subject: [PATCH] Correct the outbound HTTP examples and the email project listing --- .../official-site/sqlpage/migrations/40_fetch.sql | 13 ++++++------- .../sqlpage/migrations/58_fetch_with_meta.sql | 5 ++--- .../sqlpage/migrations/62_example_cards.sql | 2 +- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/examples/official-site/sqlpage/migrations/40_fetch.sql b/examples/official-site/sqlpage/migrations/40_fetch.sql index 0118eaa49..6995054c7 100644 --- a/examples/official-site/sqlpage/migrations/40_fetch.sql +++ b/examples/official-site/sqlpage/migrations/40_fetch.sql @@ -21,7 +21,7 @@ We use the simplest form of the fetch function, that takes the URL to fetch as a ```sql -set url = ''https://nominatim.openstreetmap.org/search?format=json&q='' || sqlpage.url_encode($user_search) +set url = ''https://nominatim.openstreetmap.org/search?format=json&q='' || sqlpage.url_encode($user_search); set api_results = sqlpage.fetch($url); select ''map'' as component; @@ -74,8 +74,7 @@ set request = json_object( set api_results = sqlpage.fetch($request); ``` -> This will add the `Authorization: Basic bXlfdXNlcm5hbWU6bXlfcGFzc3dvcmQK` header to the request, -> where `bXlfdXNlcm5hbWU6bXlfcGFzc3dvcmQK` is the base64 encoding of the string `my_username:my_password`. +SQLPage builds the `Authorization` header from the supplied username and password. ## JSON parameter format @@ -97,13 +96,13 @@ The response headers are not available for inspection. ## Conditional data fetching Since v0.40, `sqlpage.fetch(null)` returns null instead of throwing an error. -This makes it easier to conditionnally query an API: +Use it to skip the request when the value is already cached: ```sql set current_field_value = (select field from my_table where id = 1); -set target_url = nullif(''http://example.com/api/field/1'', null); -- null if the field is currently null in the db -set api_value = sqlpage.fetch($target_url); -- no http request made if the field is not null in the db -update my_table set field = $api_value where id = 1 and $api_value is not null; -- update the field only if it was not present before +set target_url = case when $current_field_value is null then ''https://example.com/api/field/1'' end; +set api_value = sqlpage.fetch($target_url); +update my_table set field = $api_value where id = 1 and field is null and $api_value is not null; ``` You can also make the HTTP request depend on whether a database query returns a row. diff --git a/examples/official-site/sqlpage/migrations/58_fetch_with_meta.sql b/examples/official-site/sqlpage/migrations/58_fetch_with_meta.sql index c406c3f7a..e89196e7a 100644 --- a/examples/official-site/sqlpage/migrations/58_fetch_with_meta.sql +++ b/examples/official-site/sqlpage/migrations/58_fetch_with_meta.sql @@ -45,7 +45,7 @@ the function returns a JSON object with an "error" field containing the error me set response = sqlpage.fetch_with_meta(''https://pokeapi.co/api/v2/pokemon/ditto''); -- redirect the user to an error page if the request failed -select ''redirect'' as component, ''error.sql'' as url +select ''redirect'' as component, ''error.sql'' as link where json_extract($response, ''$.error'') is not null or json_extract($response, ''$.status'') != 200; @@ -54,8 +54,7 @@ where select ''card'' as component; select json_extract($response, ''$.json_body.name'') as title, - json_extract($response, ''$.json_body.abilities[0].ability.name'') as description -from $response; + json_extract($response, ''$.json_body.abilities[0].ability.name'') as description; ``` ### Example: Advanced Request with Authentication diff --git a/examples/official-site/sqlpage/migrations/62_example_cards.sql b/examples/official-site/sqlpage/migrations/62_example_cards.sql index 04a7c90d9..be880031b 100644 --- a/examples/official-site/sqlpage/migrations/62_example_cards.sql +++ b/examples/official-site/sqlpage/migrations/62_example_cards.sql @@ -58,7 +58,7 @@ select 'Sending Emails', 'sending emails', 'sqlite', - 'Use the fetch function to send emails (or interact with any other HTTP API).' + 'Send email and attachments through SMTP with sqlpage.send_mail, and inspect the messages in a local Mailpit inbox.' union all select 'Simple Website',