如何在Sinatra应用程序中获取所有的URL参数

时间:2021-03-06 10:25:42

Using the following Sinatra app

使用以下Sinatra应用

get '/app' do
  content_type :json
  {"params" => params}.to_json
end

Invoking:

调用:

/app?param1=one&param2=two&param2=alt

/应用程序? param1 = one&param2 = two&param2 = alt

Gives the following result:

给出了以下结果:

{"params":{"param1":"one","param2":"alt"}}

{ " params ":{“param1”:“一”,“param2”:“alt”} }

Params has only two keys, param1 & param2.

Params只有两个键:param1和param2。

I understand Sinatra is setting params as a hash, but it does not represent all of the URL request.

我理解Sinatra将params设置为散列,但是它并不代表所有的URL请求。

Is there a way in Sinatra to get a list of all URL parameters sent in the request?

在Sinatra中是否有一种方法可以获取在请求中发送的所有URL参数的列表?

4 个解决方案

#1


18  

I believe by default params of the same name will be overwritten by the param that was processed last.

我相信在默认情况下,相同名称的params会被最后处理的param覆盖。

You could either setup params2 as an array of sorts

你可以将params2设置成某种类型的数组

...&param2[]=two&param2[]=alt

Or parse the query string vs the Sinatra provided params hash.

或者解析查询字符串vs Sinatra提供的params散列。

#2


20  

Any request in rack

任何请求在架子上

get '/app' do
  params = request.env['rack.request.query_hash']
end

#3


9  

kwon suggests to parse the query string. You can use CGI to parse it as follows:

kwon建议解析查询字符串。您可以使用CGI来解析它:

require 'cgi'

get '/app' do
  content_type :json
  {"params" => CGI::parse(request.query_string)}.to_json
end

Invoking:

调用:

/app?param1=one&param2=two&param2=alt

/应用程序? param1 = one&param2 = two&param2 = alt

Gives the following result:

给出了以下结果:

{"params":{"param1":["one"],"param2":["two","alt"]}}

{ " params ":{“param1”:“一”,“param2”:[“两个”,“alt”]} }

#4


4  

You can create a helper to make the process more friendly:

您可以创建一个帮助程序,使过程更加友好:

require 'cgi'

helpers do      
  def request_params_repeats
    params = {}
    request.env["rack.input"].read.split('&').each do |pair|
      kv = pair.split('=').map{|v| CGI.unescape(v)}
      params.merge!({kv[0]=> kv.length > 1 ? kv[1] : nil }) {|key, o, n| o.is_a?(Array) ? o << n : [o,n]}
    end
    params
  end
end

You can then access the parameters in your get block:

然后您可以访问您的get块中的参数:

get '/app' do
  content_type :json
  request_params_repeats.to_json
end

#1


18  

I believe by default params of the same name will be overwritten by the param that was processed last.

我相信在默认情况下,相同名称的params会被最后处理的param覆盖。

You could either setup params2 as an array of sorts

你可以将params2设置成某种类型的数组

...&param2[]=two&param2[]=alt

Or parse the query string vs the Sinatra provided params hash.

或者解析查询字符串vs Sinatra提供的params散列。

#2


20  

Any request in rack

任何请求在架子上

get '/app' do
  params = request.env['rack.request.query_hash']
end

#3


9  

kwon suggests to parse the query string. You can use CGI to parse it as follows:

kwon建议解析查询字符串。您可以使用CGI来解析它:

require 'cgi'

get '/app' do
  content_type :json
  {"params" => CGI::parse(request.query_string)}.to_json
end

Invoking:

调用:

/app?param1=one&param2=two&param2=alt

/应用程序? param1 = one&param2 = two&param2 = alt

Gives the following result:

给出了以下结果:

{"params":{"param1":["one"],"param2":["two","alt"]}}

{ " params ":{“param1”:“一”,“param2”:[“两个”,“alt”]} }

#4


4  

You can create a helper to make the process more friendly:

您可以创建一个帮助程序,使过程更加友好:

require 'cgi'

helpers do      
  def request_params_repeats
    params = {}
    request.env["rack.input"].read.split('&').each do |pair|
      kv = pair.split('=').map{|v| CGI.unescape(v)}
      params.merge!({kv[0]=> kv.length > 1 ? kv[1] : nil }) {|key, o, n| o.is_a?(Array) ? o << n : [o,n]}
    end
    params
  end
end

You can then access the parameters in your get block:

然后您可以访问您的get块中的参数:

get '/app' do
  content_type :json
  request_params_repeats.to_json
end