Skip to content

Commit 9b0ccbc

Browse files
committed
Keep request body and variable references in Markdown
1 parent bf5b3a0 commit 9b0ccbc

5 files changed

Lines changed: 153 additions & 186 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
icon: http-post
3+
since: 0.33.0
4+
category: request-data
5+
summary: Read the raw request body as text.
6+
---
7+
8+
Returns the raw request body as a string.
9+
10+
A client (like a web browser, mobile app, or another server) can send information to your server in the request body.
11+
This function allows you to read that information in your SQL code,
12+
in order to create or update a resource in your database for instance.
13+
14+
The request body is commonly used when building **REST APIs** (machines-to-machines interfaces)
15+
that receive data from the client.
16+
17+
This is especially useful in:
18+
- `POST` and `PUT` requests for creating or updating resources in your database
19+
- Any API endpoint that needs to receive complex data
20+
21+
### Example: Building a REST API
22+
23+
Here's an example of building an API endpoint that receives a json object,
24+
and inserts it into a database.
25+
26+
#### `api/create_user.sql`
27+
```sql
28+
-- Get the raw JSON body
29+
set user_data = sqlpage.request_body();
30+
31+
-- Insert the user into database
32+
with parsed_data as (
33+
select
34+
json_extract($user_data, '$.name') as name,
35+
json_extract($user_data, '$.email') as email
36+
)
37+
insert into users (name, email)
38+
select name, email from parsed_data;
39+
40+
-- Return success response
41+
select 'json' as component,
42+
json_object(
43+
'status', 'success',
44+
'message', 'User created successfully'
45+
) as contents;
46+
```
47+
48+
### Testing the API
49+
50+
You can test this API using curl:
51+
```bash
52+
curl -X POST http://localhost:8080/api/create_user \
53+
-H "Content-Type: application/json" \
54+
-d '{"name": "John", "email": "john@example.com"}'
55+
```
56+
57+
## Special cases
58+
59+
### NULL
60+
61+
This function returns NULL if:
62+
- There is no request body
63+
- The request content type is `application/x-www-form-urlencoded` or `multipart/form-data`
64+
(in these cases, use [`sqlpage.variables('post')`](?function=variables) instead)
65+
66+
### Binary data
67+
68+
If the request body is not valid text encoded in UTF-8,
69+
invalid characters are replaced with the Unicode replacement character `�` (U+FFFD).
70+
71+
If you need to handle binary data,
72+
use [`sqlpage.request_body_base64()`](?function=request_body_base64) instead.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
icon: photo-up
3+
since: 0.33.0
4+
category: request-data
5+
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)

examples/official-site/sqlpage/migrations/20_variables_function.sql renamed to examples/official-site/content/functions/variables.md

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
-- 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.
1517

1618
## Variable Types
1719

@@ -26,9 +28,9 @@ For more information about SQLPage variables, see the [*SQL in SQLPage* guide](/
2628
## Usage
2729

2830
- `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`
3234

3335
When a SET variable has the same name as a GET or POST variable, the SET variable takes precedence in the combined result.
3436

@@ -39,13 +41,13 @@ When a SET variable has the same name as a GET or POST variable, the SET variabl
3941
We can create a form which has a field for each value in a given table like this:
4042

4143
```sql
42-
select ''form'' as component, ''handle_survey_answer.sql'' as action;
44+
select 'form' as component, 'handle_survey_answer.sql' as action;
4345
select question_id as name, question_text as label from survey_questions;
4446
```
4547

4648
### Handling form responses using `sqlpage.variables`
4749

48-
In `handle_survey_answer.sql`, one can process the form results even if we don''t know in advance
50+
In `handle_survey_answer.sql`, one can process the form results even if we don't know in advance
4951
how many fields it contains.
5052
The function to parse JSON data varies depending on the database engine you use.
5153

@@ -54,7 +56,7 @@ In SQLite, one can use [`json_each`](https://www.sqlite.org/json1.html#jeach) :
5456

5557
```sql
5658
insert into survey_answers(question_id, answer)
57-
select "key", "value" from json_each(sqlpage.variables(''post''))
59+
select "key", "value" from json_each(sqlpage.variables('post'))
5860
```
5961

6062
#### In Postgres
@@ -64,7 +66,7 @@ Postgres has [`json_each_text`](https://www.postgresql.org/docs/9.3/functions-js
6466
```sql
6567
INSERT INTO survey_answers (question_id, answer)
6668
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);
6870
```
6971

7072

@@ -73,7 +75,7 @@ FROM json_each_text(sqlpage.variables(''post'')::json);
7375
```sql
7476
INSERT INTO survey_answers
7577
SELECT [key] AS question_id, [value] AS answer
76-
FROM OPENJSON(sqlpage.variables(''post''));
78+
FROM OPENJSON(sqlpage.variables('post'));
7779
```
7880

7981
#### In MySQL
@@ -88,32 +90,12 @@ SELECT
8890
question_id,
8991
json_unquote(
9092
json_extract(
91-
sqlpage.variables(''post''),
92-
concat(''$."'', question_id, ''"'')
93+
sqlpage.variables('post'),
94+
concat('$."', question_id, '"')
9395
)
9496
)
9597
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 int path '$')
98100
) as question_ids
99101
```
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.',
118-
'TEXT'
119-
);

‎examples/official-site/sqlpage/migrations/55_request_body.sql‎

Lines changed: 0 additions & 135 deletions
This file was deleted.

0 commit comments

Comments
 (0)