Introduced in SQLPage 0.33.0.
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.
What is Base64?
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.
Example: Handling Binary Data in an API
This example shows how to receive and process an image uploaded directly in the request body:
-- Assuming this is api/upload_image.sql
-- Client would send a POST request with the raw image data
-- Get the base64-encoded image data
set image_data = sqlpage.request_body_base64();
-- Store the image data in the database
insert into images (data, uploaded_at)
values ($image_data, current_timestamp);
-- Return success response
select 'json' as component,
json_object(
'status', 'success',
'message', 'Image uploaded successfully'
) as contents;
You can test this API using curl:
curl -X POST http://localhost:8080/api/upload_image.sql \
-H "Content-Type: application/octet-stream" \
--data-binary "@/path/to/image.jpg"
This is particularly useful when:
- Working with binary data (images, files, etc.)
- The request body contains non-UTF8 characters
- You need to pass the raw body to another system that expects base64
Note: Like
sqlpage.request_body()
, this function returns NULL if:
- There is no request body
- The request content type is
application/x-www-form-urlencoded
ormultipart/form-data
(in these cases, usesqlpage.variables('post')
instead)
basic_auth_password
basic_auth_username
client_ip
cookie
current_working_directory
environment_variable
exec
fetch
fetch_with_meta
hash_password
header
headers
link
path
persist_uploaded_file
protocol
random_string
read_file_as_data_url
read_file_as_text
request_body
request_body_base64
request_method
run_sql
uploaded_file_mime_type
uploaded_file_name
uploaded_file_path
url_encode
variables
version