如何使用:Bottle 路线中的冒号字符
How to use : colon char in Bottle route
我有简单的 Bottle 路由,URL 应该包含 :
(冒号)符号。 URL 应该像 /REST/item:128
或 /REST/item:89753
我的路线是
@route('/REST/item:<id:int>')
def icc(id):
return { 'id': id }
路由运行不正常。 id
只包含来自 url id 的最后一个字符,而不是完整的 id。
如何在路由中使用:
(冒号)?
哇,这让人困惑。
我没有时间完全了解发生了什么,但我怀疑 Bottle 的一个路由正则表达式在路由中有冒号时吃掉了太多字符。
无论如何,用反斜杠转义冒号似乎可以解决问题:
@route(r'/REST/item\:<id_:int>') # note the "r" prefix there
def icc(id_):
return {'id': id_}
这是一个测试请求及其响应:
=> curl -v 'http://127.0.0.1:8080/REST/item:123'
{"id": 123}
编辑:谜团solved。
Bottle currently supports two syntaxes for url wildcards: The
one (since 0.10) and the :old syntax. Both are described here:
http://bottlepy.org/docs/dev/routing.html
In your example, the : triggered the old syntax. The solution is to
escape the colon with a backslash (as described in the SO answer).
Escaping is fully implemented and works as intended, but is
undocumented. This is why I leave this issue open. Pull requests for
better documentation would be welcomed.
我有简单的 Bottle 路由,URL 应该包含 :
(冒号)符号。 URL 应该像 /REST/item:128
或 /REST/item:89753
我的路线是
@route('/REST/item:<id:int>')
def icc(id):
return { 'id': id }
路由运行不正常。 id
只包含来自 url id 的最后一个字符,而不是完整的 id。
如何在路由中使用:
(冒号)?
哇,这让人困惑。
我没有时间完全了解发生了什么,但我怀疑 Bottle 的一个路由正则表达式在路由中有冒号时吃掉了太多字符。
无论如何,用反斜杠转义冒号似乎可以解决问题:
@route(r'/REST/item\:<id_:int>') # note the "r" prefix there
def icc(id_):
return {'id': id_}
这是一个测试请求及其响应:
=> curl -v 'http://127.0.0.1:8080/REST/item:123'
{"id": 123}
编辑:谜团solved。
Bottle currently supports two syntaxes for url wildcards: The one (since 0.10) and the :old syntax. Both are described here: http://bottlepy.org/docs/dev/routing.html
In your example, the : triggered the old syntax. The solution is to escape the colon with a backslash (as described in the SO answer). Escaping is fully implemented and works as intended, but is undocumented. This is why I leave this issue open. Pull requests for better documentation would be welcomed.