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)
2528set api_results = sqlpage .fetch ($url);
2629
27- select ' ' map' ' as component;
30+ select ' map' as component;
2831select $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
3538In this example, we use the complex form of the function to make an
3639authenticated 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.
3942See [ the list of SQL databases and their JSON functions] ( /blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide ) for
4043more information on how to build JSON objects in your database.
4144
4245``` sql
4346set 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);
5457set api_results = sqlpage .fetch ($request);
5558
56- select ' ' code' ' as component;
59+ select ' code' as component;
5760select
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
6972set 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);
7477set api_results = sqlpage .fetch ($request);
7578```
@@ -101,7 +104,7 @@ This makes it easier to conditionnally query an API:
101104
102105``` sql
103106set 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
105108set api_value = sqlpage .fetch ($target_url); -- no http request made if the field is not null in the db
106109update 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
125128If you need to handle errors or inspect the response headers or the status code,
126129use [ ` 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- );
0 commit comments