Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a Sinatra URL as parameter in route?

I am using Sinatra routes and I would like to interpret, if possible, a normal HTTP address as parameter in a route:

url http://somesite/blog/archives   

where the route is:

/http://somesite/blog/archives 

The code is:

get '/:url' do |u|  
(some code dealing with url)

The various '/' in the HTTP URL are creating problems.

The workaround I found is passing only the URL part represented by 'somesite' in the example above, and then using:

get '/:url' do |u|
buildUrl = "http://#{u}/blog/archives"
(some code dealing with url)

Is there a way to deal directly with the full URL?

like image 552
blackbird014 Avatar asked Dec 10 '25 10:12

blackbird014


1 Answers

This will not work as you specified. As you noticed, the slashes cause trouble. What you can do instead is pass the URL as a querystring param instead of part of the URL.

get '/example' do
  url = params[:url]
  # do code with url
end

Then you can crawl whatever you want by sending your data to http://yoursite.com/example?url=http://example.com/blog/archives

like image 103
AlexQueue Avatar answered Dec 12 '25 01:12

AlexQueue