@@ -10,17 +10,16 @@ VALUES (
1010 ' transfer-vertical' ,
1111 ' Sends an HTTP request and returns detailed metadata about the response, including status code, headers, and body.
1212
13- This function is similar to `fetch`, but returns a JSON object containing detailed information about the response.
13+ This function is similar to [ `fetch`](?function=fetch) , but returns a JSON object containing detailed information about the response.
1414The returned object has the following structure:
1515```json
1616{
1717 "status": 200,
1818 "headers": {
19- "content-type": "application/json",
20- "content-length": "1234",
21- ...
19+ "content-type": "text/html",
20+ "content-length": "1234"
2221 },
23- "body": "response body content",
22+ "body": "a string, or a json object, depending on the content type ",
2423 "error": "error message if any"
2524}
2625```
@@ -32,64 +31,28 @@ the function returns a JSON object with an "error" field containing the error me
3231
3332```sql
3433-- Make a request and get detailed response information
35- set response = sqlpage.fetch_with_meta(' ' https://api.example.com/data ' ' );
34+ set response = sqlpage.fetch_with_meta(' ' https://pokeapi.co/api/v2/pokemon/ditto ' ' );
3635
37- -- Check if the request was successful
38- select case
39- when json_extract($response, ' ' $.error' ' ) is not null then
40- ' ' Request failed: ' ' || json_extract($response, ' ' $.error' ' )
41- when json_extract($response, ' ' $.status' ' ) != 200 then
42- ' ' Request returned status ' ' || json_extract($response, ' ' $.status' ' )
43- else
44- ' ' Request successful' '
45- end as message;
36+ -- redirect the user to an error page if the request failed
37+ select ' ' redirect' ' as component, ' ' error.sql' ' as url
38+ where
39+ json_extract($response, ' ' $.error' ' ) is not null
40+ or json_extract($response, ' ' $.status' ' ) != 200;
4641
47- -- Display response headers
48- select ' ' code' ' as component,
49- ' ' Response Headers' ' as title,
50- ' ' json' ' as language,
51- json_extract($response, ' ' $.headers' ' ) as contents;
52- ```
53-
54- ### Example: Error Handling with Retries
55-
56- ```sql
57- -- Function to make a request with retries
58- create temp table if not exists make_request as
59- with recursive retry(attempt, response) as (
60- -- First attempt
61- select 1 as attempt,
62- sqlpage.fetch_with_meta(' ' https://api.example.com/data' ' ) as response
63- union all
64- -- Retry up to 3 times if we get a 5xx error
65- select attempt + 1,
66- sqlpage.fetch_with_meta(' ' https://api.example.com/data' ' )
67- from retry
68- where attempt < 3
69- and (
70- json_extract(response, ' ' $.error' ' ) is not null
71- or cast(json_extract(response, ' ' $.status' ' ) as integer) >= 500
72- )
73- )
74- select response
75- from retry
76- where json_extract(response, ' ' $.error' ' ) is null
77- and cast(json_extract(response, ' ' $.status' ' ) as integer) < 500
78- limit 1;
79-
80- -- Use the response
81- select case
82- when $response is null then ' ' All retry attempts failed' '
83- else ' ' Request succeeded after retries' '
84- end as message;
42+ -- Extract data from the response json body
43+ select ' ' card' ' as component;
44+ select
45+ json_extract($response, ' ' $.body.name' ' ) as title,
46+ json_extract($response, ' ' $.body.abilities[0].ability.name' ' ) as description
47+ from $response;
8548```
8649
8750### Example: Advanced Request with Authentication
8851
8952```sql
9053set request = json_object(
9154 ' ' method' ' , ' ' POST' ' ,
92- ' ' url' ' , ' ' https://api.example. com/data ' ' ,
55+ ' ' url' ' , ' ' https://sqlpage.free.beeceptor. com' ' ,
9356 ' ' headers' ' , json_object(
9457 ' ' Content-Type' ' , ' ' application/json' ' ,
9558 ' ' Authorization' ' , ' ' Bearer ' ' || sqlpage.environment_variable(' ' API_TOKEN' ' )
@@ -101,14 +64,10 @@ set request = json_object(
10164set response = sqlpage.fetch_with_meta($request);
10265
10366-- Check response content type
104- select case
105- when json_extract($response, ' ' $.headers.content-type' ' ) like ' ' %application/json%' '
106- then json_extract($response, ' ' $.body' ' )
107- else null
108- end as json_response;
67+ select ' ' debug' ' as component, $response as response;
10968```
11069
111- The function accepts the same parameters as the `fetch` function. See the documentation of ` fetch` for more details about the available parameters .'
70+ The function accepts the same parameters as the [ `fetch` function](?function= fetch) .'
11271 );
11372
11473INSERT INTO sqlpage_function_parameters (
0 commit comments