You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 9b0ccbc
Browse filesBrowse the repository at this point in the historyBrowse files
summary: Read a binary request body as a Base64 string.
6
+
---
7
+
8
+
Returns the raw request body encoded in base64. This is useful when receiving binary data or when you need to handle non-text content in your API endpoints.
9
+
10
+
## Base64 encoding
11
+
12
+
Base64 is a way to encode binary data (like images or files) into text that can be safely stored and transmitted. This function automatically converts the incoming request body into this format.
13
+
14
+
## Receiving binary data in an API
15
+
16
+
This example shows how to receive and process an image uploaded directly in the request body:
17
+
18
+
```sql
19
+
-- Assuming this is api/upload_image.sql
20
+
-- Client would send a POST request with the raw image data
21
+
22
+
-- Get the base64-encoded image data
23
+
set image_data =sqlpage.request_body_base64();
24
+
25
+
-- Store the image data in the database
26
+
insert into images (data, uploaded_at)
27
+
values ($image_data, current_timestamp);
28
+
29
+
-- Return success response
30
+
select'json'as component,
31
+
json_object(
32
+
'status', 'success',
33
+
'message', 'Image uploaded successfully'
34
+
) as contents;
35
+
```
36
+
37
+
You can test this API using curl:
38
+
```bash
39
+
curl -X POST http://localhost:8080/api/upload_image.sql \
40
+
-H "Content-Type: application/octet-stream" \
41
+
--data-binary "@/path/to/image.jpg"
42
+
```
43
+
44
+
This is particularly useful when:
45
+
- Working with binary data (images, files, etc.)
46
+
- The request body contains non-UTF8 characters
47
+
- You need to pass the raw body to another system that expects base64
48
+
49
+
> Note: Like [`sqlpage.request_body()`](?function=request_body), this function returns NULL if:
50
+
> - There is no request body
51
+
> - The request content type is `application/x-www-form-urlencoded` or `multipart/form-data`
52
+
> (in these cases, use [`sqlpage.variables('post')`](?function=variables) instead)
-- Insert the 'variables' function into sqlpage_functions table
2
-
INSERT INTO sqlpage_functions (
3
-
"name",
4
-
"introduced_in_version",
5
-
"icon",
6
-
"description_md"
7
-
)
8
-
VALUES (
9
-
'variables',
10
-
'0.15.0',
11
-
'variable',
12
-
'Returns a JSON string containing variables from the HTTP request and user-defined variables.
13
-
14
-
The [database''s json handling functions](/blog?post=JSON+in+SQL%3A+A+Comprehensive+Guide) can then be used to process the data.
1
+
---
2
+
icon: variable
3
+
since: 0.15.0
4
+
category: request-data
5
+
summary: Read request or page variables as a JSON object.
6
+
parameters:
7
+
- name: method
8
+
type: TEXT
9
+
description: 'Optional. Filter variables by source: ''get'' (URL parameters), ''post'' (form data), or
10
+
''set'' (user-defined variables). When not provided, all variables are returned with SET variables taking
11
+
precedence over request parameters.'
12
+
---
13
+
14
+
Returns a JSON string containing variables from the HTTP request and user-defined variables.
15
+
16
+
The [database's json handling functions](/blog?post=JSON+in+SQL%3A+A+Comprehensive+Guide) can then be used to process the data.
15
17
16
18
## Variable Types
17
19
@@ -26,9 +28,9 @@ For more information about SQLPage variables, see the [*SQL in SQLPage* guide](/
26
28
## Usage
27
29
28
30
-`sqlpage.variables()` - returns all variables (GET, POST, and SET combined). When multiple variables of the same name are present, the order of precedence is: set > post > get.
29
-
- `sqlpage.variables(''get'')` - returns only URL parameters
30
-
- `sqlpage.variables(''post'')` - returns only POST form data
31
-
- `sqlpage.variables(''set'')` - returns only user-defined variables created with `SET`
31
+
-`sqlpage.variables('get')` - returns only URL parameters
32
+
-`sqlpage.variables('post')` - returns only POST form data
33
+
-`sqlpage.variables('set')` - returns only user-defined variables created with `SET`
32
34
33
35
When a SET variable has the same name as a GET or POST variable, the SET variable takes precedence in the combined result.
34
36
@@ -39,13 +41,13 @@ When a SET variable has the same name as a GET or POST variable, the SET variabl
39
41
We can create a form which has a field for each value in a given table like this:
40
42
41
43
```sql
42
-
select ''form'' as component, ''handle_survey_answer.sql'' as action;
@@ -64,7 +66,7 @@ Postgres has [`json_each_text`](https://www.postgresql.org/docs/9.3/functions-js
64
66
```sql
65
67
INSERT INTO survey_answers (question_id, answer)
66
68
SELECT key AS question_id, value AS answer
67
-
FROM json_each_text(sqlpage.variables(''post'')::json);
69
+
FROM json_each_text(sqlpage.variables('post')::json);
68
70
```
69
71
70
72
@@ -73,7 +75,7 @@ FROM json_each_text(sqlpage.variables(''post'')::json);
73
75
```sql
74
76
INSERT INTO survey_answers
75
77
SELECT [key] AS question_id, [value] AS answer
76
-
FROM OPENJSON(sqlpage.variables(''post''));
78
+
FROM OPENJSON(sqlpage.variables('post'));
77
79
```
78
80
79
81
#### In MySQL
@@ -88,32 +90,12 @@ SELECT
88
90
question_id,
89
91
json_unquote(
90
92
json_extract(
91
-
sqlpage.variables(''post''),
92
-
concat(''$."'', question_id, ''"'')
93
+
sqlpage.variables('post'),
94
+
concat('$."', question_id, '"')
93
95
)
94
96
)
95
97
FROM json_table(
96
-
json_keys(sqlpage.variables(''post'')),
97
-
''$[*]'' columns (question_id int path ''$'')
98
+
json_keys(sqlpage.variables('post')),
99
+
'$[*]' columns (question_id intpath'$')
98
100
) as question_ids
99
101
```
100
-
101
-
'
102
-
);
103
-
104
-
-- Insert the parameters for the 'variables' function into sqlpage_function_parameters table
105
-
-- Parameter 1: 'method' parameter
106
-
INSERT INTO sqlpage_function_parameters (
107
-
"function",
108
-
"index",
109
-
"name",
110
-
"description_md",
111
-
"type"
112
-
)
113
-
VALUES (
114
-
'variables',
115
-
1,
116
-
'method',
117
-
'Optional. Filter variables by source: ''get'' (URL parameters), ''post'' (form data), or ''set'' (user-defined variables). When not provided, all variables are returned with SET variables taking precedence over request parameters.',
0 commit comments