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
Addresses issue #2962 by adding detailed documentation for:
- Dynamic path parameter syntax (<parameter_name>)
- Supported characters in path parameters
- Regex pattern conversion process
- Advanced routing examples with complex parameters
- Catch-all routes with regex patterns
- Route matching priority and behavior
The new documentation provides clear explanations of how routing rules work,
the regex syntax supported, and examples of complex routing patterns that
were previously undocumented.
Copy file name to clipboardExpand all lines: docs/core/event_handler/api_gateway.md
+132-4Lines changed: 132 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -214,17 +214,145 @@ Each dynamic route you set must be part of your function signature. This allows
214
214
???+ tip
215
215
You can also nest dynamic paths, for example `/todos/<todo_id>/<todo_status>`.
216
216
217
+
#### Routing Rules Syntax
218
+
219
+
The routing system uses a specific syntax to define dynamic URL patterns. Understanding this syntax is crucial for creating flexible and robust API routes.
220
+
221
+
##### Dynamic Path Parameters
222
+
223
+
Dynamic path parameters are defined using angle brackets `<parameter_name>` syntax. These parameters are automatically converted to regex patterns for efficient route matching.
224
+
225
+
**Syntax**: `/path/<parameter_name>`
226
+
227
+
***Parameter names** must contain only word characters (letters, numbers, underscore)
228
+
***Captured values** can contain letters, numbers, underscores, and these special characters: `-._~()'!*:@,;=+&$%<> \[]{}|^`
229
+
230
+
=== "routing_syntax_basic.py"
231
+
232
+
```python
233
+
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
234
+
235
+
app = APIGatewayRestResolver()
236
+
237
+
@app.get("/users/<user_id>")
238
+
def get_user(user_id: str):
239
+
# user_id can be: "123", "user-456", "john.doe", "user_with_underscores"
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.
302
+
217
303
#### Catch-all routes
218
304
219
305
???+ note
220
306
We recommend having explicit routes whenever possible; use catch-all routes sparingly.
221
307
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 `.+`.
308
+
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.
223
309
224
-
You can also combine nested paths with greedy regex to catch in between routes.
310
+
##### Using Regex Patterns
225
311
226
-
???+ warning
227
-
We choose the most explicit registered route that matches an incoming event.
312
+
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:
313
+
314
+
| Pattern | Description | Examples |
315
+
|---------|-------------|----------|
316
+
|`.+`| Matches one or more characters (greedy) |`/proxy/.+` matches `/proxy/any/deep/path`|
317
+
|`.*`| Matches zero or more characters (greedy) |`/files/.*` matches `/files/` and `/files/deep/path`|
318
+
|`[^/]+`| Matches one or more non-slash characters |`/api/[^/]+` matches `/api/v1` but not `/api/v1/users`|
319
+
|`\w+`| Matches one or more word characters |`/users/\w+` matches `/users/john123`|
320
+
321
+
**Common Regex Route Examples:**
322
+
323
+
```python
324
+
# File path proxy - captures everything after /files/
0 commit comments