Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions examples/official-site/sqlpage/migrations/40_fetch.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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

Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Loading