-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
232 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
INSERT INTO sqlpage_functions ( | ||
"name", | ||
"introduced_in_version", | ||
"icon", | ||
"description_md" | ||
) | ||
VALUES ( | ||
'fetch', | ||
'0.20.3', | ||
'transfer-vertical', | ||
'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. | ||
```sql | ||
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. | ||
```sql | ||
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; | ||
``` | ||
' | ||
); | ||
INSERT INTO sqlpage_function_parameters ( | ||
"function", | ||
"index", | ||
"name", | ||
"description_md", | ||
"type" | ||
) | ||
VALUES ( | ||
'fetch', | ||
1, | ||
'url', | ||
'Either a string containing an URL to request, or a json object in the standard format of the request interface of the web fetch API.', | ||
'TEXT' | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
set res = sqlpage.fetch('{ | ||
"method": "POST", | ||
"url": "http://localhost:62802/post", | ||
"headers": {"x-custom": "1"}, | ||
"body": {"hello": "world"} | ||
}'); | ||
set expected_like = 'POST /post | ||
accept-encoding: br, gzip, deflate, zstd | ||
content-length: 18 | ||
content-type: application/json | ||
date: % | ||
host: localhost:62802 | ||
x-custom: 1 | ||
{"hello": "world"}'; | ||
select 'text' as component, | ||
case | ||
when $res LIKE $expected_like then 'It works !' | ||
else 'It failed ! Expected: | ||
' || $expected_like || 'Got: | ||
' || $res | ||
end as contents; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
set res = sqlpage.fetch('http://localhost:62802/hello_world') | ||
select 'text' as component, | ||
case | ||
when $res LIKE 'GET /hello_world%' then 'It works !' | ||
else 'It failed ! Got: ' || $res | ||
end as contents; |