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
- The request content type is `application/x-www-form-urlencoded` or `multipart/form-data`
67
+
(in these cases, use [`sqlpage.variables(''post'')`](?function=variables) instead)
68
+
69
+
### Binary data
70
+
71
+
If the request body is not valid text encoded in UTF-8,
72
+
invalid characters are replaced with the Unicode replacement character `�` (U+FFFD).
73
+
74
+
If you need to handle binary data,
75
+
use [`sqlpage.request_body_base64()`](?function=request_body_base64) instead.
76
+
'
77
+
);
78
+
79
+
INSERT INTO sqlpage_functions (
80
+
"name",
81
+
"introduced_in_version",
82
+
"icon",
83
+
"description_md"
84
+
)
85
+
VALUES (
86
+
'request_body_base64',
87
+
'0.33.0',
88
+
'photo-up',
89
+
'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.
90
+
91
+
### What is Base64?
92
+
93
+
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.
94
+
95
+
### Example: Handling Binary Data in an API
96
+
97
+
This example shows how to receive and process an image uploaded directly in the request body:
98
+
99
+
```sql
100
+
-- Assuming this is api/upload_image.sql
101
+
-- Client would send a POST request with the raw image data
102
+
103
+
-- Get the base64-encoded image data
104
+
set image_data = sqlpage.request_body_base64();
105
+
106
+
-- Store the image data in the database
107
+
insert into images (data, uploaded_at)
108
+
values ($image_data, current_timestamp);
109
+
110
+
-- Return success response
111
+
select ''json'' as component,
112
+
json_object(
113
+
''status'', ''success'',
114
+
''message'', ''Image uploaded successfully''
115
+
) as contents;
116
+
```
117
+
118
+
You can test this API using curl:
119
+
```bash
120
+
curl -X POST http://localhost:8080/api/upload_image.sql \
121
+
-H "Content-Type: application/octet-stream" \
122
+
--data-binary "@/path/to/image.jpg"
123
+
```
124
+
125
+
This is particularly useful when:
126
+
- Working with binary data (images, files, etc.)
127
+
- The request body contains non-UTF8 characters
128
+
- You need to pass the raw body to another system that expects base64
129
+
130
+
> Note: Like [`sqlpage.request_body()`](?function=request_body), this function returns NULL if:
131
+
> - There is no request body
132
+
> - The request content type is `application/x-www-form-urlencoded` or `multipart/form-data`
133
+
> (in these cases, use [`sqlpage.variables(''post'')`](?function=variables) instead)
0 commit comments