我有一个要求,用户可以用这种格式传递网址:
http:site.com/[action]/[subject]?[option]=[value]
例如以下所有内容都是有效的网址:
http://example.com/trigger/a/b/c
http://example.com/trigger/a
http://example.com/trigger/a/b?something=value
http://example.com/trigger/any/thing/goes/here
我有grape
这样的资源:
class Test < Grape::API
params do
optional :option, type: String
end
get '/trigger/:a/:b/:c' do
{
type: 'trigger',
a: params[:a],
b: params[:b],
c: params[:c],
option: params[:option]
}
end
end
所以,如果我访问http://example.com/1/2/3/option=something
,那么我会得到
{
"type": "trigger",
"a": "1",
"b": "2",
"c": "3",
"option": "something"
}
预期行为:
使用应该能够在/trigger/
http://example.com/any/thing/goes/here/1/2/3?other=value&goes=here
更新
我找到了针对rails路由的此解决方案(How do we identify parameters in a dynamic URL?),我想在grape
中使用此行为。
感谢
答案 0 :(得分:0)
嗯,实际上我正在寻找wildcards
matching params
get "trigger/*subject" do
{
params: params[:subject]
}
end
现在它将响应路径:
curl http://example.com/trigger/subject/can/be/anything
输出:
{
params: "subject/can/be/anything"
}
感谢Neil的回答 Wildcard route in Grape