Skip to content

Commit 7f74091

Browse files
committed
Move the request-body and variables references to Markdown
1 parent 0c160be commit 7f74091

5 files changed

Lines changed: 154 additions & 187 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: 29 additions & 47 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: >-
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.
1517

1618
## Variable Types
1719

@@ -26,12 +28,12 @@ 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
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"`.
3537
The combined `sqlpage.variables()` contains `"page":null`.
3638

3739
## Example: a form with a variable number of fields
@@ -41,13 +43,13 @@ The combined `sqlpage.variables()` contains `"page":null`.
4143
We can create a form which has a field for each value in a given table like this:
4244

4345
```sql
44-
select ''form'' as component, ''handle_survey_answer.sql'' as action;
46+
select 'form' as component, 'handle_survey_answer.sql' as action;
4547
select question_id as name, question_text as label from survey_questions;
4648
```
4749

4850
### Handling form responses using `sqlpage.variables`
4951

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

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

5759
```sql
5860
insert into survey_answers(question_id, answer)
59-
select "key", "value" from json_each(sqlpage.variables(''post''))
61+
select "key", "value" from json_each(sqlpage.variables('post'))
6062
```
6163

6264
#### In Postgres
@@ -66,7 +68,7 @@ Postgres has [`json_each_text`](https://www.postgresql.org/docs/9.3/functions-js
6668
```sql
6769
INSERT INTO survey_answers (question_id, answer)
6870
SELECT key AS question_id, value AS answer
69-
FROM json_each_text(sqlpage.variables(''post'')::json);
71+
FROM json_each_text(sqlpage.variables('post')::json);
7072
```
7173

7274

@@ -75,7 +77,7 @@ FROM json_each_text(sqlpage.variables(''post'')::json);
7577
```sql
7678
INSERT INTO survey_answers
7779
SELECT [key] AS question_id, [value] AS answer
78-
FROM OPENJSON(sqlpage.variables(''post''));
80+
FROM OPENJSON(sqlpage.variables('post'));
7981
```
8082

8183
#### In MySQL
@@ -90,32 +92,12 @@ SELECT
9092
question_id,
9193
json_unquote(
9294
json_extract(
93-
sqlpage.variables(''post''),
94-
concat(''$."'', question_id, ''"'')
95+
sqlpage.variables('post'),
96+
concat('$."', question_id, '"')
9597
)
9698
)
9799
FROM json_table(
98-
json_keys(sqlpage.variables(''post'')),
99-
''$[*]'' columns (question_id int path ''$'')
100+
json_keys(sqlpage.variables('post')),
101+
'$[*]' columns (question_id int path '$')
100102
) as question_ids
101103
```
102-
103-
'
104-
);
105-
106-
-- Insert the parameters for the 'variables' function into sqlpage_function_parameters table
107-
-- Parameter 1: 'method' parameter
108-
INSERT INTO sqlpage_function_parameters (
109-
"function",
110-
"index",
111-
"name",
112-
"description_md",
113-
"type"
114-
)
115-
VALUES (
116-
'variables',
117-
1,
118-
'method',
119-
'Filter by source: `get` for URL parameters, `post` for form fields, or `set` for page variables. With no argument, the result combines all three.',
120-
'TEXT'
121-
);

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

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

0 commit comments

Comments
 (0)