简体   繁体   中英

Get full path in Sinatra route including everything after question mark

I have the following path:

http://192.168.56.10:4567/browse/foo/bar?x=100&y=200

I want absolutely everything that comes after "http://192.168.56.10:4567/browse/" in a string.

Using a splat doesn't work (only catches "foo/bar"):

get '/browse/*' do

Neither does the regular expression (also only catches "foo/bar"):

get %r{/browse/(.*)} do

The x and y params are all accessible in the params hash, but doing a .map on the ones I want seems unreasonable and un-ruby-like (also, this is just an example.. my params are actually very dynamic and numerous). Is there a better way to do this?

More info: my path looks this way because it is communicating with an API and I use the route to determine the API call I will make. I need the string to look this way.

If you are willing to ignore hash tag in path param this should work(BTW browser would ignore anything after hash in URL)

updated answer

get "/browse/*" do
  p "#{request.path}?#{request.query_string}".split("browse/")[1]
end

Or even simpler

request.fullpath.split("browse/")[1]
get "/browse/*" do
  a = "#{params[:splat]}?#{request.env['rack.request.query_string']}"
  "Got #{a}"
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM