Skip to content

Commit 63e7eb8

Browse files
committed
Added docs and example about **params
1 parent a56a507 commit 63e7eb8

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

docs/examples.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ In the example below the second route has only one URL parameter, so the ``actio
180180
Keep in mind that URL parameters are always passed as strings, so you need to convert them to the desired type.
181181
Also note that the names of the function parameters **have to match** with the ones used in route, but they **do not have to** be in the same order.
182182

183+
Alternatively you can use e.g. ``**params`` to get all the parameters as a dictionary and access them using ``params['parameter_name']``.
184+
183185
It is also possible to specify a wildcard route:
184186

185187
- ``...`` - matches one path segment, e.g ``/api/...`` will match ``/api/123``, but **not** ``/api/123/456``
@@ -189,7 +191,7 @@ In both cases, wildcards will not match empty path segment, so ``/api/.../users`
189191

190192
.. literalinclude:: ../examples/httpserver_url_parameters.py
191193
:caption: examples/httpserver_url_parameters.py
192-
:emphasize-lines: 30-34,53-54
194+
:emphasize-lines: 30-34,53-54,65-66
193195
:linenos:
194196

195197
Authentication

examples/httpserver_url_parameters.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ def perform_action(
5050
)
5151

5252

53+
@server.route("/device/<device_id>/status/<date>")
54+
def device_status_on_date(request: Request, **params: dict):
55+
"""
56+
Return the status of a specified device between two dates.
57+
"""
58+
59+
device_id = params.get("device_id")
60+
date = params.get("date")
61+
62+
return Response(request, f"Status of {device_id} on {date}: ...")
63+
64+
5365
@server.route("/device/.../status", append_slash=True)
5466
@server.route("/device/....", append_slash=True)
5567
def device_status(request: Request):

0 commit comments

Comments
 (0)