SQLPage built-in functions
In addition to normal SQL functions supported by your database, SQLPage provides a few special functions to help you extract data from user requests.
These functions are special, because they are not executed inside your database, but by SQLPage itself before sending the query to your database. Thus, they require all the parameters to be known at the time the query is sent to your database. Function parameters cannot reference columns from the rest of your query.
SQLPage functions
The sqlpage.fetch function
Introduced in SQLPage 0.20.3.
Sends an HTTP request and returns the results as a string.
Example
Simple GET query
In this example, we use an API call to find the latitude and longitude of a place the user searched for, and we display it on a map.
We use the simplest form of the fetch function, that takes the URL to fetch as a string.
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;
select $user_search as title,
CAST($api_results->>0->>'lat' AS FLOAT) as latitude,
CAST($api_results->>0->>'lon' AS FLOAT) as longitude;
POST query with a body
In this example, we use the complex form of the function to make an authenticated POST request, with custom request headers and a custom request body.
We use SQLite's json functions to build the request body.
set request = json_object(
'method', 'POST'
'url', 'https://postman-echo.com/post',
'headers', json_object(
'Content-Type', 'application/json',
'Authorization', 'Bearer ' || sqlpage.environment_variable('MY_API_TOKEN')
),
'body', json_object(
'Hello', 'world',
),
);
set api_results = sqlpage.fetch($request);
select 'code' as component;
select
'API call results' as title,
'json' as language,
$api_results as contents;