Sinatra and question mark
In a URL, a question mark separates the path part from the query part. The query part normally consists of name/value pairs, and is often constructed by a web browser to match the data a user has entered into a form. For example a url might look like:
http://example.com/submit?name=John&age=93
Here the path section in /submit
, and the query sections is name=John&age=93
which refers to the value “John” for the name
key, and “93” for the age
.
When you create a route in Sinatra, you only specify the path part. Sinatra then parses the query, and makes the data in it available in the params
object. In this example you could do something like this:
get '/submit' do
name = params[:name]
age = params[:age]
# use name and age variables
...
end
If you use a ?
character when defining a Sinatra route, it makes part of the url optional. In the example you used (get "/add?:string_to_add"
), it will actually match any url starting with /ad
, then optionally another d
, and then anything else will be put in the :string_to_add
key of the params hash, and the query section will be parsed separately. In other words the question mark makes the preceding d
character optional.
If you want to get the ‘raw’ text of the query string in Sinatra, you can use the query_string
method of the request
object. In your example that would look something like this:
get '/add' do
string_to_add = request.query_string
...
end
Note that the route doesn’t include the ?
character, just the base /add
.
Sinatra URL Matching with question mark?
You can’t directly do what you want. When describing the route, Sinatra treats ?
as defining an optional parameter, and doesn’t provide a way to escape it.
In a url a ?
separates the path from the query string, and Sinatra only uses the path when matching routes. The contents of the query string are parsed and available in params
, and the raw string is available as request.query_string
.
Your url scheme seems rather unusual, but one possibility if you want to avoid checking the query_string
in the actual route could be to create a custom condition to apply to the routes, and check in there:
set(:query) { |val| condition { request.query_string == val } }
get '/:id', :query => 'inspect' do
# ...
end
get '/:id', :query => 'last' do
# ...
end
get '/:id' do
# ...
end
Get full path in Sinatra route including everything after question mark
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]
Sinatra binary return for msgpack -- charset issue/ characters being converted somewhere?
0x82
is LOW QUOTATION MARK
in Latin1, 0x201a
is the same character in UTF-16. Have a look at how your libraries deal with encoding, tell them to use a binary encoding and not try any conversion between encodings.
UTF-16 smells of JavaScript. If you use jQuery, have a look at http://blog.vjeux.com/2011/javascript/jquery-binary-ajax.html.
In Sinatra, how do i pass control to a named route?
You can either use redirect
, if you actually want the URL in the user's browser to change, or use call!
directly if you want the forwarding to be hidden from the end user (i.e. the URL in their browser stays the same, but internally the request is forwarded to a different route in your application).
So in your case, it would either be
redirect "/old/#{params[:title]}" if params[:ext] == 'php'
or
call! env.merge('PATH_INFO' => "/old/#{params[:title]}") if params[:ext] == 'php'
Sinatra optional parameter operator and syntax. What is the right syntax?
The right syntax is:
get '/foo/?:bar?' do
"Hi"
end
Where first question mark makes preceding /
optional and second makes preceding symbol :bar
optional, so that:/foo/about
- yes/foo/about/blog
- no/foo
- yes/foo/
- yes
Otherwise you can make only :bar
symbol optional with
get '/foo/:bar?' do
"Hi"
end
so that:/foo/about
- yes/foo/about/blog
- no/foo
- no/foo/
- yes
To give you one more example, this option:
get '/go/?:bar' do
"wow"
end
is practically equivalent to this:
get '/go/:bar' do
"wow"
end
Sinatra with optional query parameters
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.
How to use params with slashes with Sinatra?
Did you try to use splat parameters?
Something like:
get '/add/*' do
protocol = params[:splat].first
address = params[:splat][1..-1].join('/')
url = protocol + "//" + address
end
Related Topics
Using Bsearch to Find Index for Inserting New Element into Sorted Array
How to Click on a Specific Coordinates of an Element
How to Convert a Ruby String with Brackets to an Array
Ruby on Rails 4 Select Multiple
Does Ruby Have Syntax for Safe Navigation Operator of Nil Values, Like in Groovy
Obtaining a Facebook Auth Token for a Command-Line (Desktop) Application
Is Regexp.Last_Match Thread Safe
Missing File in Gem After Build
How to Get the Final Url After Redirects Using Ruby
Add Existing Classes into a Module
How to Get the Array Index or Iteration Number with an Each Iterator
How to Write to File When Using Marshal::Dump in Ruby for Object Serialization
Bootstrap Datepicker Default Value Simple_Form_For
Ruby: How to Find the Key of the Largest Value in a Hash
How to Find If Range Is Contained in an Array of Ranges