![](/img/trans.png)
[英]How can we get path params in falcon middleware, if any path param in the route?
[英]Parse uuid from parameterized route path in falcon
我有一个falcon
应用程序,其中包含用于获取资源的参数化路径。 用户不知道资源的uuid
,因为它是临时的,因此需要重定向。
用户将发出GET /事务请求,并重定向到返回的路径302找到响应。
如何解析请求路径中的uuid?
该应用程序将如下所示:
api = falcon.API()
api.add_route('/transaction', Transaction)
api.add_route('/transaction/{id}', TransactionItem))
并且资源类似于:
class Transaction(object):
def on_get(self, req, resp):
id = get_current_id()
resp.status = falcon.HTTPFound('/TransactionItem/{}'.format(id))
class TransactionItem(object):
def on_get(self, req, resp):
// Parse id from path?
transaction = get_transaction(id)
// ...
// include info in the response, etc
resp.status = falcon.HTTP_200
好的
Flacon将匹配的路径字段作为关键字参数传递。 这意味着你的TransactionItem
类中的on_get
你的on_get
必须有一个给定定义的 (你可以选择一个对你更清楚的):
# 1st way
def on_get(self, req, resp, id=None):
...
# 2nd way (**kwargs catches all keywords args)
def on_get(self, req, resp, **kwargs):
id = kwargs.get('id')
如果你想通过猎鹰转换它,传递的字段将被作为str
传递dafault你可以使用Falcon UUIDConverter
的内置
这里是转换器的文档: https : //falcon.readthedocs.io/en/stable/api/routing.html#falcon.routing.UUIDConverter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.