0

I have a requirement where user can pass url in this format:

http:site.com/[action]/[subject]?[option]=[value]

for example all of the below are valid urls:

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

I have a grape resource like this:

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

So, If I visit http://example.com/1/2/3/option=something then I will get

{
  "type": "trigger",
  "a": "1",
  "b": "2",
  "c": "3",
  "option": "something"
}

Expected Behavior:

Use should be able to provide anything after /trigger/

http://example.com/any/thing/goes/here/1/2/3?other=value&goes=here

Update:

I found this solution (How do we identify parameters in a dynamic URL?) for rails route, I want this behavior in grape.

thanks

Community
  • 1
  • 1
przbadu
  • 5,769
  • 5
  • 42
  • 67

1 Answers1

0

Well, actually I was looking for wildcards with matching params

get "trigger/*subject" do
  {
    params: params[:subject]
  }
end

Now it will respond to path like:

curl http://example.com/trigger/subject/can/be/anything

Output:

{
 params: "subject/can/be/anything"
}

Thanks to Neil's answer Wildcard route in Grape

Community
  • 1
  • 1
przbadu
  • 5,769
  • 5
  • 42
  • 67