Files
sqlpage--sqlpage/examples/official-site/sqlpage/migrations/58_fetch_with_meta.sql
T
wehub-resource-sync d718c5a372
CI / compile_and_lint (push) Failing after 0s
CI / docker_build (linux/amd64, -linux-amd64-duckdb, duckdb) (push) Failing after 0s
CI / docker_build (linux/arm64, -linux-arm64, minimal) (push) Failing after 2s
CI / docker_build (linux/arm64, -linux-arm64-duckdb, duckdb) (push) Failing after 1s
CI / docker_build (linux/amd64, minimal) (push) Failing after 1s
CI / test (, sqlite, sqlite::memory:) (push) Has been skipped
CI / test (mssql, mssql, mssql://root:Password123!@127.0.0.1/sqlpage) (push) Has been skipped
CI / test (mysql, mysql, mysql://root:Password123!@127.0.0.1/sqlpage) (push) Has been skipped
CI / test (oracle, oracle, Driver=Oracle 21 ODBC driver;Dbq=//127.0.0.1:1521/FREEPDB1;Uid=root;Pwd=Password123!) (push) Has been skipped
CI / test (postgres, odbc, Driver=PostgreSQL Unicode;Server=127.0.0.1;Port=5432;Database=sqlpage;UID=root;PWD=Password123!, true) (push) Has been skipped
CI / test (postgres, postgres, postgres://root:Password123!@127.0.0.1/sqlpage) (push) Has been skipped
CI / playwright (push) Has been skipped
CI / docker_build (linux/arm/v7, -linux-arm-v7, minimal) (push) Failing after 0s
CI / hurl_examples (push) Failing after 8s
deploy website / deploy_official_site (push) Failing after 1s
CI / hurl (${{ matrix.example }}) (push) Has been skipped
CI / docker_push (duckdb) (push) Has been cancelled
CI / docker_push (minimal) (push) Has been cancelled
CI / windows_test (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:31:57 +08:00

86 lines
2.5 KiB
SQL

INSERT INTO sqlpage_functions (
"name",
"introduced_in_version",
"icon",
"description_md"
)
VALUES (
'fetch_with_meta',
'0.34.0',
'transfer-vertical',
'Sends an HTTP request and returns detailed metadata about the response, including status code, headers, and body.
This function is similar to [`fetch`](?function=fetch), but returns a JSON object containing detailed information about the response.
The returned object has the following structure:
```json
{
"status": 200,
"headers": {
"content-type": "text/html",
"content-length": "1234"
},
"body": "a string, or a json object, depending on the content type",
"error": "error message if any"
}
```
If the request fails or encounters an error (e.g., network issues, invalid UTF-8 response), instead of throwing an error,
the function returns a JSON object with an "error" field containing the error message.
### Example: Basic Usage
```sql
-- Make a request and get detailed response information
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
where
json_extract($response, ''$.error'') is not null
or json_extract($response, ''$.status'') != 200;
-- Extract data from the response json body
select ''card'' as component;
select
json_extract($response, ''$.body.name'') as title,
json_extract($response, ''$.body.abilities[0].ability.name'') as description
from $response;
```
### Example: Advanced Request with Authentication
```sql
set request = json_object(
''method'', ''POST'',
''url'', ''https://sqlpage.free.beeceptor.com'',
''headers'', json_object(
''Content-Type'', ''application/json'',
''Authorization'', ''Bearer '' || sqlpage.environment_variable(''API_TOKEN'')
),
''body'', json_object(
''key'', ''value''
)
);
set response = sqlpage.fetch_with_meta($request);
-- Check response content type
select ''debug'' as component, $response as response;
```
The function accepts the same parameters as the [`fetch` function](?function=fetch).'
);
INSERT INTO sqlpage_function_parameters (
"function",
"index",
"name",
"description_md",
"type"
)
VALUES (
'fetch_with_meta',
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'
);