Ping.Goblue
Ping.Goblue

Reputation: 606

Get Sinatra request route/path

Is there a way that I can get the request route in Sinatra. For example, I have a route:

get "/:id/post" do
  # whatever
end

When I hit that endpoint, for example, "/abc/post", I want to log the route: "/:id/post", instead of "/abc/post", which I can get via request.path_info.

Any help will be appreciated.

Upvotes: 2

Views: 914

Answers (1)

Jordan Running
Jordan Running

Reputation: 106027

The router stores the matched route in @env["sinatra.route"], so you can do this:

get '/:id/post' do
  logger.info "Route: #{@env["sinatra.route"]}"
  # ...
end

This will log:

I, [2016-12-05T10:59:36.678467 #35615]  INFO -- : Route: GET /:id/post

Upvotes: 4

Related Questions