Skip to content

Commit 901cb0d

Browse files
committed
Keep fetch function examples in Markdown
1 parent 0b08b23 commit 901cb0d

4 files changed

Lines changed: 118 additions & 146 deletions

File tree

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
INSERT INTO sqlpage_functions (
2-
"name",
3-
"introduced_in_version",
4-
"icon",
5-
"description_md"
6-
)
7-
VALUES (
8-
'fetch',
9-
'0.20.3',
10-
'transfer-vertical',
11-
'Sends an HTTP request and returns the results as a string.
1+
---
2+
icon: transfer-vertical
3+
since: 0.20.3
4+
category: external-services
5+
summary: Send an HTTP request and read the response body.
6+
parameters:
7+
- name: url
8+
type: TEXT
9+
required: true
10+
description: Either a string containing an URL to request, or a json object in the standard format of the
11+
request interface of the web fetch API.
12+
---
13+
14+
Sends an HTTP request and returns the results as a string.
1215

1316
## HTTP request examples
1417

@@ -21,55 +24,55 @@ We use the simplest form of the fetch function, that takes the URL to fetch as a
2124

2225

2326
```sql
24-
set url = ''https://nominatim.openstreetmap.org/search?format=json&q='' || sqlpage.url_encode($user_search)
27+
set url = 'https://nominatim.openstreetmap.org/search?format=json&q=' || sqlpage.url_encode($user_search)
2528
set api_results = sqlpage.fetch($url);
2629

27-
select ''map'' as component;
30+
select 'map' as component;
2831
select $user_search as title,
29-
CAST($api_results->>0->>''lat'' AS FLOAT) as latitude,
30-
CAST($api_results->>0->>''lon'' AS FLOAT) as longitude;
32+
CAST($api_results->>0->>'lat' AS FLOAT) as latitude,
33+
CAST($api_results->>0->>'lon' AS FLOAT) as longitude;
3134
```
3235

3336
### POST query with a body
3437

3538
In this example, we use the complex form of the function to make an
3639
authenticated POST request, with custom request headers and a custom request body.
3740

38-
We use SQLite''s json functions to build the request body.
41+
We use SQLite's json functions to build the request body.
3942
See [the list of SQL databases and their JSON functions](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide) for
4043
more information on how to build JSON objects in your database.
4144

4245
```sql
4346
set request = json_object(
44-
''method'', ''POST'',
45-
''url'', ''https://postman-echo.com/post'',
46-
''headers'', json_object(
47-
''Content-Type'', ''application/json'',
48-
''Authorization'', ''Bearer '' || sqlpage.environment_variable(''MY_API_TOKEN'')
47+
'method', 'POST',
48+
'url', 'https://postman-echo.com/post',
49+
'headers', json_object(
50+
'Content-Type', 'application/json',
51+
'Authorization', 'Bearer ' || sqlpage.environment_variable('MY_API_TOKEN')
4952
),
50-
''body'', json_object(
51-
''Hello'', ''world''
53+
'body', json_object(
54+
'Hello', 'world'
5255
)
5356
);
5457
set api_results = sqlpage.fetch($request);
5558

56-
select ''code'' as component;
59+
select 'code' as component;
5760
select
58-
''API call results'' as title,
59-
''json'' as language,
61+
'API call results' as title,
62+
'json' as language,
6063
$api_results as contents;
6164
```
6265

6366

6467
### Authenticated request using Basic Auth
6568

66-
Here''s how to make a request to an API that requires [HTTP Basic Authentication](https://en.wikipedia.org/wiki/Basic_access_authentication):
69+
Here's how to make a request to an API that requires [HTTP Basic Authentication](https://en.wikipedia.org/wiki/Basic_access_authentication):
6770

6871
```sql
6972
set request = json_object(
70-
''url'', ''https://api.example.com/data'',
71-
''username'', ''my_username'',
72-
''password'', ''my_password''
73+
'url', 'https://api.example.com/data',
74+
'username', 'my_username',
75+
'password', 'my_password'
7376
);
7477
set api_results = sqlpage.fetch($request);
7578
```
@@ -101,7 +104,7 @@ This makes it easier to conditionnally query an API:
101104

102105
```sql
103106
set current_field_value = (select field from my_table where id = 1);
104-
set target_url = nullif(''http://example.com/api/field/1'', null); -- null if the field is currently null in the db
107+
set target_url = nullif('http://example.com/api/field/1', null); -- null if the field is currently null in the db
105108
set api_value = sqlpage.fetch($target_url); -- no http request made if the field is not null in the db
106109
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
107110
```
@@ -124,19 +127,3 @@ If the query returns more than one row or more than one column, SQLPage returns
124127

125128
If you need to handle errors or inspect the response headers or the status code,
126129
use [`sqlpage.fetch_with_meta`](?function=fetch_with_meta).
127-
'
128-
);
129-
INSERT INTO sqlpage_function_parameters (
130-
"function",
131-
"index",
132-
"name",
133-
"description_md",
134-
"type"
135-
)
136-
VALUES (
137-
'fetch',
138-
1,
139-
'url',
140-
'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.',
141-
'TEXT'
142-
);
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
icon: transfer-vertical
3+
since: 0.34.0
4+
category: external-services
5+
summary: Send an HTTP request and read its status, headers, and body.
6+
parameters:
7+
- name: url
8+
type: TEXT
9+
required: true
10+
description: Either a string containing an URL to request, or a json object in the standard format of the
11+
request interface of the web fetch API.
12+
---
13+
14+
Sends an HTTP request and returns detailed metadata about the response, including status code, headers, and body.
15+
16+
This function is similar to [`fetch`](?function=fetch), but returns a JSON object containing detailed information about the response.
17+
When the response declares a `content-type` of `application/json`, the parsed body is returned under `json_body`:
18+
```json
19+
{
20+
"status": 200,
21+
"headers": {
22+
"content-type": "application/json",
23+
"content-length": "1234"
24+
},
25+
"json_body": { "name": "ditto" }
26+
}
27+
```
28+
29+
For every other content type, the body is returned as a string under `body`:
30+
```json
31+
{
32+
"status": 200,
33+
"headers": {
34+
"content-type": "text/html",
35+
"content-length": "1234"
36+
},
37+
"body": "<html>...</html>"
38+
}
39+
```
40+
41+
If the request fails or encounters an error (e.g., network issues, invalid UTF-8 response), instead of throwing an error,
42+
the function returns a JSON object with an "error" field containing the error message.
43+
44+
### Example: Basic Usage
45+
46+
```sql
47+
-- Make a request and get detailed response information
48+
set response = sqlpage.fetch_with_meta('https://pokeapi.co/api/v2/pokemon/ditto');
49+
50+
-- redirect the user to an error page if the request failed
51+
select 'redirect' as component, 'error.sql' as url
52+
where
53+
json_extract($response, '$.error') is not null
54+
or json_extract($response, '$.status') != 200;
55+
56+
-- Extract data from the response json body
57+
select 'card' as component;
58+
select
59+
json_extract($response, '$.json_body.name') as title,
60+
json_extract($response, '$.json_body.abilities[0].ability.name') as description
61+
from $response;
62+
```
63+
64+
### Example: Advanced Request with Authentication
65+
66+
```sql
67+
set request = json_object(
68+
'method', 'POST',
69+
'url', 'https://sqlpage.free.beeceptor.com',
70+
'headers', json_object(
71+
'Content-Type', 'application/json',
72+
'Authorization', 'Bearer ' || sqlpage.environment_variable('API_TOKEN')
73+
),
74+
'body', json_object(
75+
'key', 'value'
76+
)
77+
);
78+
set response = sqlpage.fetch_with_meta($request);
79+
80+
-- Check response content type
81+
select 'debug' as component, $response as response;
82+
```
83+
84+
The function accepts the same parameters as the [`fetch` function](?function=fetch).

‎examples/official-site/sqlpage/migrations/58_fetch_with_meta.sql‎

Lines changed: 0 additions & 97 deletions
This file was deleted.

‎examples/official-site/sqlpage/migrations/82_reference_catalog.sql‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ WITH copy(name, category, summary) AS (VALUES
8989

9090

9191

92-
('fetch', 'external-services', 'Send an HTTP request and read the response body.'),
93-
('fetch_with_meta', 'external-services', 'Send an HTTP request and read its status, headers, and body.'),
9492
('send_mail', 'external-services', 'Send email with optional attachments through an SMTP server.'),
9593

9694
('configuration_directory', 'server-and-runtime', 'Locate the directory for SQLPage configuration and templates.'),

0 commit comments

Comments
 (0)