16

I want to create a Sinatra API route with optional query parameters.I'm able to add the query parameters as follows

%r{^/mysql/data/(?)/start_time=(?\w*)/?}

But the route corresponding to the above route is like "/mysql/data/:name/start_time=:start_time"

I need the query parameters as optional and to be declared in URL format.

Eg:

/mysql/data/:name?start_time=:start_time&end_time=:end_time

Is there any way in Sinatra to do this?

NagaLakshmi
  • 715
  • 3
  • 12
  • 24

1 Answers1

25

Quoting from the Sinatra Docs:

# Routes may also utilize query parameters:

get '/posts' do
  # matches "GET /posts?title=foo&author=bar"
  title = params[:title]
  author = params[:author]
  # uses title and author variables; query is optional to the /posts route
end

In your case simply use /mysql/data/:name, any query parameters will be available via params automatically.

branch14
  • 1,253
  • 11
  • 24