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/core/event_handler/api_gateway.md
+49-16Lines changed: 49 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -196,8 +196,8 @@ You can use `/todos/<todo_id>` to configure dynamic URL paths, where `<todo_id>`
196
196
197
197
Each dynamic route you set must be part of your function signature. This allows us to call your function using keyword arguments when matching your dynamic route.
198
198
199
-
???+ note
200
-
For brevity, we will only include the necessary keys for each sample request for the example to work.
199
+
???+ tip
200
+
You can also nest dynamic paths, for example `/todos/<todo_id>/<todo_status>`.
201
201
202
202
=== "dynamic_routes.py"
203
203
@@ -211,32 +211,65 @@ Each dynamic route you set must be part of your function signature. This allows
You can also nest dynamic paths, for example `/todos/<todo_id>/<todo_status>`.
214
+
#### Dynamic path mechanism
215
+
216
+
Dynamic path parameters are defined using angle brackets `<parameter_name>` syntax. These parameters are automatically converted to regex patterns for efficient route matching and performance gains.
217
+
218
+
**Syntax**: `/path/<parameter_name>`
219
+
220
+
***Parameter names** must contain only word characters (letters, numbers, underscore)
221
+
***Captured values** can contain letters, numbers, underscores, and these special characters: `-._~()'!*:@,;=+&$%<> \[]{}|^`. Reserved characters must be percent-encoded in URLs to prevent errors.
The parameter names in your route (`<user_id>`) must exactly match the parameter names in your function signature (`user_id: str`). This is how the framework knows which captured values to pass to which parameters.
216
244
217
245
#### Catch-all routes
218
246
219
-
???+ note
220
-
We recommend having explicit routes whenever possible; use catch-all routes sparingly.
247
+
For scenarios where you need to handle arbitrary or deeply nested paths, you can use regex patterns directly in your route definitions. These are particularly useful for proxy routes or when dealing with file paths.
221
248
222
-
You can use a [regex](https://docs.python.org/3/library/re.html#regular-expression-syntax){target="_blank" rel="nofollow"} string to handle an arbitrary number of paths within a request, for example `.+`.
249
+
**We recommend** having explicit routes whenever possible; use catch-all routes sparingly.
223
250
224
-
You can also combine nested paths with greedy regex to catch in between routes.
251
+
##### Using Regex Patterns
225
252
226
-
???+ warning
227
-
We choose the most explicit registered route that matches an incoming event.
253
+
You can use standard [Python regex patterns](https://docs.python.org/3/library/re.html#regular-expression-syntax){target="_blank" rel="nofollow"} in your route definitions, for example:
254
+
255
+
| Pattern | Description | Examples |
256
+
|---------|-------------|----------|
257
+
|`.+`| Matches one or more characters (greedy) |`/proxy/.+` matches `/proxy/any/deep/path`|
258
+
|`.*`| Matches zero or more characters (greedy) |`/files/.*` matches `/files/` and `/files/deep/path`|
259
+
|`[^/]+`| Matches one or more non-slash characters |`/api/[^/]+` matches `/api/v1` but not `/api/v1/users`|
260
+
|`\w+`| Matches one or more word characters |`/users/\w+` matches `/users/john123`|
0 commit comments