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 7f74091
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: >-
10
+
Filter by source: `get` for URL parameters, `post` for form fields, or `set` for page variables.
11
+
With no argument, the result combines all three.
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,12 +28,12 @@ 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
For example, after `SET page = NULL` on a request to `?page=2`,
34
-
`sqlpage.variables(''get'')` still contains `"page":"2"`.
36
+
`sqlpage.variables('get')` still contains `"page":"2"`.
35
37
The combined `sqlpage.variables()` contains `"page":null`.
36
38
37
39
## Example: a form with a variable number of fields
@@ -41,13 +43,13 @@ The combined `sqlpage.variables()` contains `"page":null`.
41
43
We can create a form which has a field for each value in a given table like this:
42
44
43
45
```sql
44
-
select ''form'' as component, ''handle_survey_answer.sql'' as action;
0 commit comments