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
Copy file name to clipboardExpand all lines: docs/guide/web/route.md
+61-44Lines changed: 61 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,22 @@
1
-
# Route
1
+
# Route and Endpoint
2
2
3
-
**Route** is the main entrypoint that handles an HTTP request. All routes are defined in the `routes` directory.
3
+
**Route** is the main entry point that handles an HTTP requestin AIScript.
4
4
5
-
## Define a route
5
+
## Define An Endpoint
6
6
7
-
Define a route is simple and straightforward. The syntax is:
7
+
Defining an endpoint is simple and straightforward. The syntax is:
8
8
9
9
```
10
-
verb path [(arg1: type1, arg2: type2, ...)] {
10
+
verb path [, verb path...] {
11
+
// query parameters
12
+
query {
13
+
// parameter definitions
14
+
}
11
15
16
+
// request body
17
+
body {
18
+
// body field definitions
19
+
}
12
20
}
13
21
```
14
22
@@ -30,12 +38,14 @@ get /hello {
30
38
}
31
39
```
32
40
33
-
```
41
+
```bash
34
42
$ curl http://localhost:8080/hello
35
43
Hello World
36
44
```
37
45
38
-
## Parse query string
46
+
## Parse Query String
47
+
48
+
Use the `query` block to declare fields in the query string. You can set a default value to make a parameter optional. To access query parameters, use the `query.name` or `query["name"]` syntax.
Each field declared in the `query` block is a query parameter, the`str`is the type of the query parameter. You can also specify a default value for the query parameter.
66
+
Each field declared in the `query` block is a query parameter, with`str`indicating the parameter type.
57
67
58
68
```bash
59
69
$ curl http://localhost:8080/hello
60
70
Hello, Alice!
61
71
```
62
72
63
-
The `@string` is a validator. AIScript will validate the query parameter and return an error if the validation fails.
73
+
The [@string](/reference/directives#string) is a validator. AIScript will validate the query parameter and return an error if validation fails.
64
74
65
75
```bash
66
-
$ curl http://localhost:8080/hello?name=WS
76
+
$ curl http://localhost:8080/hello?name=Le
67
77
{
68
78
"error": "Invalid query parameter: name, must be between 3 and 10 characters"
69
79
}
70
80
```
71
81
72
-
## Parse request body
82
+
:::tip
83
+
For more information about validators, see [Validator](./validator.md).
84
+
:::
85
+
86
+
## Parse Request Body
73
87
74
-
```js
88
+
```py
75
89
post /hello {
76
90
@json
77
91
body {
78
92
@string(min_len=3, max_len=10)
79
93
name: str
80
94
}
81
95
82
-
return"Hello, {name}!";
96
+
return"Hello, "+ body.name+"!";
83
97
}
84
98
```
85
99
86
-
```
100
+
```bash
87
101
$ curl -X POST -H "Content-Type: application/json" \
Each field declared in the `body` block is a request body parameter, the`str`is the type of the request body parameter. The `@json` directive tells AIScript to parse the request body as JSON. Another directive is `@form`, it tells AIScript to parse the request body as form data.
106
+
Each field declared in the `body` block is a request body parameter, with`str`indicating the parameter type. The [@json](/reference/directives#json) directive tells AIScript to parse the request body as JSON. Alternatively, the [@form](/reference/directives#form) directive instructs AIScript to parse the request body as form data. Access body parameters using `body.name` or `body["name"]` syntax.
93
107
94
108
```py
95
109
post /hello {
@@ -99,48 +113,48 @@ post /hello {
99
113
name: str
100
114
}
101
115
102
-
return"Hello, {name}!";
116
+
return"Hello, "+ body.name+"!";
103
117
}
104
118
```
105
119
106
-
```
120
+
```bash
107
121
$ curl -X POST -H "Content-Type: application/x-www-form-urlencoded" \
108
122
-d 'name=AIScript' http://localhost:8080/hello
109
123
Hello, AIScript!
110
124
```
111
125
112
-
## Header and cookie
126
+
## Headers and Cookies
113
127
114
-
Access and set header and cookie is easy.
128
+
Accessing and setting headers and cookies is straightforward using the `header` and `cookie` variables, which are request-scoped.
API routes normally never a simple text response, it often need programming logic to handle the request. In AIScript route, you can write any programming logic in the route just like other web frameworks in ohter languages.
196
+
API routes typically require more than simple text responses; they often need programming logic to handle requests. In AIScript routes, you can write any programming logic just as you would in other web frameworks.
179
197
180
-
```rust
198
+
```py
181
199
get /hello {
182
200
query {
183
201
value: int
184
202
}
185
203
186
-
ifvalue>10 {
204
+
ifquery.value >10 {
187
205
return"Value is greater than 10";
188
-
else {
206
+
} else {
189
207
return"Value is less than or equal to 10";
190
208
}
191
209
}
192
210
```
193
211
194
-
```
212
+
```bash
195
213
$ curl http://localhost:8080/hello?value=11
196
214
Value is greater than 10
197
215
```
198
216
199
-
## Query database with SQL
217
+
## Query Database with SQL
200
218
201
219
```py
202
220
get /tweet/<id: int> {
@@ -205,4 +223,3 @@ get /tweet/<id: int> {
205
223
return pg.query("SELECT * FROM tweet WHERE id = $1", id);
0 commit comments