Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 643
halph Avatar asked Dec 12 '25 14:12

halph


1 Answers

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]
like image 51
ch4nd4n Avatar answered Dec 14 '25 12:12

ch4nd4n



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!