From 422bfe582d027193b8661e4aa88b5e1c89288a89 Mon Sep 17 00:00:00 2001 From: Ophir Lojkine Date: Sat, 28 Nov 2026 23:46:31 +0000 Subject: [PATCH] Move the request-body and variables references to Markdown --- .../content/functions/request_body.md | 72 ++++++++++ .../content/functions/request_body_base64.md | 52 +++++++ .../functions/variables.md} | 76 ++++------ .../sqlpage/migrations/55_request_body.sql | 135 ------------------ .../migrations/82_reference_catalog.sql | 6 +- 5 files changed, 154 insertions(+), 187 deletions(-) create mode 100644 examples/official-site/content/functions/request_body.md create mode 100644 examples/official-site/content/functions/request_body_base64.md rename examples/official-site/{sqlpage/migrations/20_variables_function.sql => content/functions/variables.md} (58%) delete mode 100644 examples/official-site/sqlpage/migrations/55_request_body.sql diff --git a/examples/official-site/content/functions/request_body.md b/examples/official-site/content/functions/request_body.md new file mode 100644 index 000000000..724a327c4 --- /dev/null +++ b/examples/official-site/content/functions/request_body.md @@ -0,0 +1,72 @@ +--- +icon: http-post +since: 0.33.0 +category: request-data +summary: Read the raw request body as text. +--- + +Returns the raw request body as a string. + +A client (like a web browser, mobile app, or another server) can send information to your server in the request body. +This function allows you to read that information in your SQL code, +in order to create or update a resource in your database for instance. + +The request body is commonly used when building **REST APIs** (machines-to-machines interfaces) +that receive data from the client. + +This is especially useful in: +- `POST` and `PUT` requests for creating or updating resources in your database +- Any API endpoint that needs to receive complex data + +### Example: Building a REST API + +Here's an example of building an API endpoint that receives a json object, +and inserts it into a database. + +#### `api/create_user.sql` +```sql +-- Get the raw JSON body +set user_data = sqlpage.request_body(); + +-- Insert the user into database +with parsed_data as ( + select + json_extract($user_data, '$.name') as name, + json_extract($user_data, '$.email') as email +) +insert into users (name, email) +select name, email from parsed_data; + +-- Return success response +select 'json' as component, + json_object( + 'status', 'success', + 'message', 'User created successfully' + ) as contents; +``` + +### Testing the API + +You can test this API using curl: +```bash +curl -X POST http://localhost:8080/api/create_user \ + -H "Content-Type: application/json" \ + -d '{"name": "John", "email": "john@example.com"}' +``` + +## Special cases + +### NULL + +This function returns NULL if: + - There is no request body + - The request content type is `application/x-www-form-urlencoded` or `multipart/form-data` + (in these cases, use [`sqlpage.variables('post')`](?function=variables) instead) + +### Binary data + +If the request body is not valid text encoded in UTF-8, +invalid characters are replaced with the Unicode replacement character `�` (U+FFFD). + +If you need to handle binary data, +use [`sqlpage.request_body_base64()`](?function=request_body_base64) instead. diff --git a/examples/official-site/content/functions/request_body_base64.md b/examples/official-site/content/functions/request_body_base64.md new file mode 100644 index 000000000..47cac0860 --- /dev/null +++ b/examples/official-site/content/functions/request_body_base64.md @@ -0,0 +1,52 @@ +--- +icon: photo-up +since: 0.33.0 +category: request-data +summary: Read a binary request body as a Base64 string. +--- + +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. + +## Base64 encoding + +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. + +## Receiving binary data in an API + +This example shows how to receive and process an image uploaded directly in the request body: + +```sql +-- 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: +```bash +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()`](?function=request_body), this function returns NULL if: +> - There is no request body +> - The request content type is `application/x-www-form-urlencoded` or `multipart/form-data` +> (in these cases, use [`sqlpage.variables('post')`](?function=variables) instead) diff --git a/examples/official-site/sqlpage/migrations/20_variables_function.sql b/examples/official-site/content/functions/variables.md similarity index 58% rename from examples/official-site/sqlpage/migrations/20_variables_function.sql rename to examples/official-site/content/functions/variables.md index a0379d774..8e00fc70f 100644 --- a/examples/official-site/sqlpage/migrations/20_variables_function.sql +++ b/examples/official-site/content/functions/variables.md @@ -1,17 +1,19 @@ --- Insert the 'variables' function into sqlpage_functions table -INSERT INTO sqlpage_functions ( - "name", - "introduced_in_version", - "icon", - "description_md" -) -VALUES ( - 'variables', - '0.15.0', - 'variable', - 'Returns a JSON string containing variables from the HTTP request and user-defined variables. - -The [database''s json handling functions](/blog?post=JSON+in+SQL%3A+A+Comprehensive+Guide) can then be used to process the data. +--- +icon: variable +since: 0.15.0 +category: request-data +summary: Read request or page variables as a JSON object. +parameters: + - name: method + type: TEXT + description: >- + Filter by source: `get` for URL parameters, `post` for form fields, or `set` for page variables. + With no argument, the result combines all three. +--- + +Returns a JSON string containing variables from the HTTP request and user-defined variables. + +The [database's json handling functions](/blog?post=JSON+in+SQL%3A+A+Comprehensive+Guide) can then be used to process the data. ## Variable Types @@ -26,12 +28,12 @@ For more information about SQLPage variables, see the [*SQL in SQLPage* guide](/ ## Usage - `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. -- `sqlpage.variables(''get'')` - returns only URL parameters -- `sqlpage.variables(''post'')` - returns only POST form data -- `sqlpage.variables(''set'')` - returns only user-defined variables created with `SET` +- `sqlpage.variables('get')` - returns only URL parameters +- `sqlpage.variables('post')` - returns only POST form data +- `sqlpage.variables('set')` - returns only user-defined variables created with `SET` For example, after `SET page = NULL` on a request to `?page=2`, -`sqlpage.variables(''get'')` still contains `"page":"2"`. +`sqlpage.variables('get')` still contains `"page":"2"`. The combined `sqlpage.variables()` contains `"page":null`. ## Example: a form with a variable number of fields @@ -41,13 +43,13 @@ The combined `sqlpage.variables()` contains `"page":null`. We can create a form which has a field for each value in a given table like this: ```sql -select ''form'' as component, ''handle_survey_answer.sql'' as action; +select 'form' as component, 'handle_survey_answer.sql' as action; select question_id as name, question_text as label from survey_questions; ``` ### Handling form responses using `sqlpage.variables` -In `handle_survey_answer.sql`, one can process the form results even if we don''t know in advance +In `handle_survey_answer.sql`, one can process the form results even if we don't know in advance how many fields it contains. The function to parse JSON data varies depending on the database engine you use. @@ -56,7 +58,7 @@ In SQLite, one can use [`json_each`](https://www.sqlite.org/json1.html#jeach) : ```sql insert into survey_answers(question_id, answer) -select "key", "value" from json_each(sqlpage.variables(''post'')) +select "key", "value" from json_each(sqlpage.variables('post')) ``` #### In Postgres @@ -66,7 +68,7 @@ Postgres has [`json_each_text`](https://www.postgresql.org/docs/9.3/functions-js ```sql INSERT INTO survey_answers (question_id, answer) SELECT key AS question_id, value AS answer -FROM json_each_text(sqlpage.variables(''post'')::json); +FROM json_each_text(sqlpage.variables('post')::json); ``` @@ -75,7 +77,7 @@ FROM json_each_text(sqlpage.variables(''post'')::json); ```sql INSERT INTO survey_answers SELECT [key] AS question_id, [value] AS answer -FROM OPENJSON(sqlpage.variables(''post'')); +FROM OPENJSON(sqlpage.variables('post')); ``` #### In MySQL @@ -90,32 +92,12 @@ SELECT question_id, json_unquote( json_extract( - sqlpage.variables(''post''), - concat(''$."'', question_id, ''"'') + sqlpage.variables('post'), + concat('$."', question_id, '"') ) ) FROM json_table( - json_keys(sqlpage.variables(''post'')), - ''$[*]'' columns (question_id int path ''$'') + json_keys(sqlpage.variables('post')), + '$[*]' columns (question_id int path '$') ) as question_ids ``` - -' -); - --- Insert the parameters for the 'variables' function into sqlpage_function_parameters table --- Parameter 1: 'method' parameter -INSERT INTO sqlpage_function_parameters ( - "function", - "index", - "name", - "description_md", - "type" -) -VALUES ( - 'variables', - 1, - 'method', - 'Filter by source: `get` for URL parameters, `post` for form fields, or `set` for page variables. With no argument, the result combines all three.', - 'TEXT' -); diff --git a/examples/official-site/sqlpage/migrations/55_request_body.sql b/examples/official-site/sqlpage/migrations/55_request_body.sql deleted file mode 100644 index 423ef6a27..000000000 --- a/examples/official-site/sqlpage/migrations/55_request_body.sql +++ /dev/null @@ -1,135 +0,0 @@ -INSERT INTO sqlpage_functions ( - "name", - "introduced_in_version", - "icon", - "description_md" - ) -VALUES ( - 'request_body', - '0.33.0', - 'http-post', - 'Returns the raw request body as a string. - -A client (like a web browser, mobile app, or another server) can send information to your server in the request body. -This function allows you to read that information in your SQL code, -in order to create or update a resource in your database for instance. - -The request body is commonly used when building **REST APIs** (machines-to-machines interfaces) -that receive data from the client. - -This is especially useful in: -- `POST` and `PUT` requests for creating or updating resources in your database -- Any API endpoint that needs to receive complex data - -### Example: Building a REST API - -Here''s an example of building an API endpoint that receives a json object, -and inserts it into a database. - -#### `api/create_user.sql` -```sql --- Get the raw JSON body -set user_data = sqlpage.request_body(); - --- Insert the user into database -with parsed_data as ( - select - json_extract($user_data, ''$.name'') as name, - json_extract($user_data, ''$.email'') as email -) -insert into users (name, email) -select name, email from parsed_data; - --- Return success response -select ''json'' as component, - json_object( - ''status'', ''success'', - ''message'', ''User created successfully'' - ) as contents; -``` - -### Testing the API - -You can test this API using curl: -```bash -curl -X POST http://localhost:8080/api/create_user \ - -H "Content-Type: application/json" \ - -d ''{"name": "John", "email": "john@example.com"}'' -``` - -## Special cases - -### NULL - -This function returns NULL if: - - There is no request body - - The request content type is `application/x-www-form-urlencoded` or `multipart/form-data` - (in these cases, use [`sqlpage.variables(''post'')`](?function=variables) instead) - -### Binary data - -If the request body is not valid text encoded in UTF-8, -invalid characters are replaced with the Unicode replacement character `�` (U+FFFD). - -If you need to handle binary data, -use [`sqlpage.request_body_base64()`](?function=request_body_base64) instead. -' - ); - -INSERT INTO sqlpage_functions ( - "name", - "introduced_in_version", - "icon", - "description_md" - ) -VALUES ( - 'request_body_base64', - '0.33.0', - 'photo-up', - '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. - -## Base64 encoding - -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. - -## Receiving binary data in an API - -This example shows how to receive and process an image uploaded directly in the request body: - -```sql --- 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: -```bash -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()`](?function=request_body), this function returns NULL if: -> - There is no request body -> - The request content type is `application/x-www-form-urlencoded` or `multipart/form-data` -> (in these cases, use [`sqlpage.variables(''post'')`](?function=variables) instead) -' - ); \ No newline at end of file diff --git a/examples/official-site/sqlpage/migrations/82_reference_catalog.sql b/examples/official-site/sqlpage/migrations/82_reference_catalog.sql index 4cd49c460..334cdef9a 100644 --- a/examples/official-site/sqlpage/migrations/82_reference_catalog.sql +++ b/examples/official-site/sqlpage/migrations/82_reference_catalog.sql @@ -33,8 +33,7 @@ UPDATE sqlpage_function_parameters SET optional = TRUE WHERE ("function", name) ('persist_uploaded_file', 'destination_folder'), ('persist_uploaded_file', 'allowed_extensions'), ('persist_uploaded_file', 'mode'), ('persist_uploaded_files', 'destination_folder'), ('persist_uploaded_files', 'allowed_extensions'), ('persist_uploaded_files', 'mode'), ('run_sql', 'parameters'), - ('set_variable', 'value'), - ('variables', 'method') + ('set_variable', 'value') ); WITH copy(name, category, summary) AS (VALUES @@ -92,9 +91,6 @@ UPDATE component SET (catalog_category, catalog_summary) = (copy.category, copy. FROM copy WHERE component.name = copy.name; WITH copy(name, category, summary) AS (VALUES - ('request_body', 'request-data', 'Read the raw request body as text.'), - ('request_body_base64', 'request-data', 'Read a binary request body as a Base64 string.'), - ('variables', 'request-data', 'Read request or page variables as a JSON object.'), ('link', 'links-and-routing', 'Build a page URL with optional query parameters and a fragment.'), ('regex_match', 'links-and-routing', 'Match text against a regular expression and extract capture groups.'),