Ruby:如何将哈希转换为HTTP参数?

时间:2009-04-28 16:06:34

标签: ruby http parameters hashmap

使用像

这样的普通哈希非常简单
{:a => "a", :b => "b"} 

将转化为

"a=a&b=b"

但你如何处理更复杂的事情,比如

{:a => "a", :b => ["c", "d", "e"]} 

应转换为

"a=a&b[0]=c&b[1]=d&b[2]=e" 

或更糟糕的是,(做什么)有类似的事情:

{:a => "a", :b => [{:c => "c", :d => "d"}, {:e => "e", :f => "f"}]

感谢非常感谢您的帮助!

16 个答案:

答案 0 :(得分:251)

对于基本的非嵌套哈希,Rails / ActiveSupport有Object#to_query

>> {:a => "a", :b => ["c", "d", "e"]}.to_query
=> "a=a&b%5B%5D=c&b%5B%5D=d&b%5B%5D=e"
>> CGI.unescape({:a => "a", :b => ["c", "d", "e"]}.to_query)
=> "a=a&b[]=c&b[]=d&b[]=e"

http://api.rubyonrails.org/classes/Object.html#method-i-to_query

答案 1 :(得分:141)

如果您使用的是Ruby 1.9.2或更高版本,如果您不需要数组,则可以使用URI.encode_www_form

E.g。 (来自1.9.3中的Ruby文档):

URI.encode_www_form([["q", "ruby"], ["lang", "en"]])
#=> "q=ruby&lang=en"
URI.encode_www_form("q" => "ruby", "lang" => "en")
#=> "q=ruby&lang=en"
URI.encode_www_form("q" => ["ruby", "perl"], "lang" => "en")
#=> "q=ruby&q=perl&lang=en"
URI.encode_www_form([["q", "ruby"], ["q", "perl"], ["lang", "en"]])
#=> "q=ruby&q=perl&lang=en"

您会注意到数组值未使用包含[]的键名设置,就像我们在查询字符串中习惯使用的一样。 encode_www_form使用的规范符合application/x-www-form-urlencoded数据的HTML5定义。

答案 2 :(得分:93)

如果您使用Rails,有一种更简单的方法: http://apidock.com/rails/ActiveSupport/CoreExtensions/Hash/to_query

所以你可以这样做:

{:a => "a", :b => "b"}.to_query

答案 3 :(得分:83)

更新:此功能已从gem中删除。

朱利安,你的自我回答是好的,我从它借来的无耻,但它没有正确地逃脱保留字符,还有一些其他边缘情况,它会崩溃。

require "addressable/uri"
uri = Addressable::URI.new
uri.query_values = {:a => "a", :b => ["c", "d", "e"]}
uri.query
# => "a=a&b[0]=c&b[1]=d&b[2]=e"
uri.query_values = {:a => "a", :b => [{:c => "c", :d => "d"}, {:e => "e", :f => "f"}]}
uri.query
# => "a=a&b[0][c]=c&b[0][d]=d&b[1][e]=e&b[1][f]=f"
uri.query_values = {:a => "a", :b => {:c => "c", :d => "d"}}
uri.query
# => "a=a&b[c]=c&b[d]=d"
uri.query_values = {:a => "a", :b => {:c => "c", :d => true}}
uri.query
# => "a=a&b[c]=c&b[d]"
uri.query_values = {:a => "a", :b => {:c => "c", :d => true}, :e => []}
uri.query
# => "a=a&b[c]=c&b[d]"

宝石是'addressable'

gem install addressable

答案 4 :(得分:55)

无需加载膨胀的ActiveSupport或滚动自己的ActiveSupport,您可以使用Rack::Utils.build_queryRack::Utils.build_nested_queryHere's a blog post给出了一个很好的例子:

require 'rack'

Rack::Utils.build_query(
  authorization_token: "foo",
  access_level: "moderator",
  previous: "index"
)

# => "authorization_token=foo&access_level=moderator&previous=index"

它甚至可以处理数组:

Rack::Utils.build_query( {:a => "a", :b => ["c", "d", "e"]} )
# => "a=a&b=c&b=d&b=e"
Rack::Utils.parse_query _
# => {"a"=>"a", "b"=>["c", "d", "e"]}

或者更难嵌套的东西:

Rack::Utils.build_nested_query( {:a => "a", :b => [{:c => "c", :d => "d"}, {:e => "e", :f => "f"}] } )
# => "a=a&b[][c]=c&b[][d]=d&b[][e]=e&b[][f]=f"
Rack::Utils.parse_nested_query _
# => {"a"=>"a", "b"=>[{"c"=>"c", "d"=>"d", "e"=>"e", "f"=>"f"}]}

答案 5 :(得分:9)

从Merb偷窃:

# File merb/core_ext/hash.rb, line 87
def to_params
  params = ''
  stack = []

  each do |k, v|
    if v.is_a?(Hash)
      stack << [k,v]
    else
      params << "#{k}=#{v}&"
    end
  end

  stack.each do |parent, hash|
    hash.each do |k, v|
      if v.is_a?(Hash)
        stack << ["#{parent}[#{k}]", v]
      else
        params << "#{parent}[#{k}]=#{v}&"
      end
    end
  end

  params.chop! # trailing &
  params
end

请参阅http://noobkit.com/show/ruby/gems/development/merb/hash/to_params.html

答案 6 :(得分:8)

如果您只需要支持简单的ASCII键/值查询字符串,那么这是一个简短而又甜蜜的内容:

hash = {"foo" => "bar", "fooz" => 123}
# => {"foo"=>"bar", "fooz"=>123}
query_string = hash.to_a.map { |x| "#{x[0]}=#{x[1]}" }.join("&")
# => "foo=bar&fooz=123"

答案 7 :(得分:4)

class Hash
  def to_params
    params = ''
    stack = []

    each do |k, v|
      if v.is_a?(Hash)
        stack << [k,v]
      elsif v.is_a?(Array)
        stack << [k,Hash.from_array(v)]
      else
        params << "#{k}=#{v}&"
      end
    end

    stack.each do |parent, hash|
      hash.each do |k, v|
        if v.is_a?(Hash)
          stack << ["#{parent}[#{k}]", v]
        else
          params << "#{parent}[#{k}]=#{v}&"
        end
      end
    end

    params.chop! 
    params
  end

  def self.from_array(array = [])
    h = Hash.new
    array.size.times do |t|
      h[t] = array[t]
    end
    h
  end

end

答案 8 :(得分:3)

{:a=>"a", :b=>"b", :c=>"c"}.map{ |x,v| "#{x}=#{v}" }.reduce{|x,v| "#{x}&#{v}" }

"a=a&b=b&c=c"

这是另一种方式。对于简单的查询。

答案 9 :(得分:2)

我知道这是一个老问题,但我只想发布这段代码,因为我找不到一个简单的宝石来为我做这个任务。

module QueryParams

  def self.encode(value, key = nil)
    case value
    when Hash  then value.map { |k,v| encode(v, append_key(key,k)) }.join('&')
    when Array then value.map { |v| encode(v, "#{key}[]") }.join('&')
    when nil   then ''
    else            
      "#{key}=#{CGI.escape(value.to_s)}" 
    end
  end

  private

  def self.append_key(root_key, key)
    root_key.nil? ? key : "#{root_key}[#{key.to_s}]"
  end
end

在这里滚动为宝石:https://github.com/simen/queryparams

答案 10 :(得分:1)

还有另一个答案,结果是:

require 'uri'
URI.encode_www_form({"one" => "value with space", "two" => ["v1", "v2"]})
# => => "one=value+with+space&two=v1&two=v2"

请注意,它会将空格转义为“ +”,这在URL / URI的技术上可能正确,也可能不正确。这一切都变得非常混乱。

答案 11 :(得分:1)

如果您处于法拉第请求的上下文中,还可以仅将params哈希作为第二个参数传递,而faraday负责使其中的适当参数URL成为一部分:

faraday_instance.get(url, params_hsh)

答案 12 :(得分:0)

我喜欢使用这个宝石:

https://rubygems.org/gems/php_http_build_query

样本用法:

puts PHP.http_build_query({"a"=>"b","c"=>"d","e"=>[{"hello"=>"world","bah"=>"black"},{"hello"=>"world","bah"=>"black"}]})

# a=b&c=d&e%5B0%5D%5Bbah%5D=black&e%5B0%5D%5Bhello%5D=world&e%5B1%5D%5Bbah%5D=black&e%5B1%5D%5Bhello%5D=world

答案 13 :(得分:0)

使用Hash.to_params的最佳方法是使用数组工作正常的方法。

{a: 1, b: [1,2,3]}.to_param
"a=1&b[]=1&b[]=2&b[]=3"

答案 14 :(得分:0)

require 'uri'

class Hash
  def to_query_hash(key)
    reduce({}) do |h, (k, v)|
      new_key = key.nil? ? k : "#{key}[#{k}]"
      v = Hash[v.each_with_index.to_a.map(&:reverse)] if v.is_a?(Array)
      if v.is_a?(Hash)
        h.merge!(v.to_query_hash(new_key))
      else
        h[new_key] = v
      end
      h
    end
  end

  def to_query(key = nil)
    URI.encode_www_form(to_query_hash(key))
  end
end

2.4.2 :019 > {:a => "a", :b => "b"}.to_query_hash(nil)
 => {:a=>"a", :b=>"b"}

2.4.2 :020 > {:a => "a", :b => "b"}.to_query
 => "a=a&b=b"

2.4.2 :021 > {:a => "a", :b => ["c", "d", "e"]}.to_query_hash(nil)
 => {:a=>"a", "b[0]"=>"c", "b[1]"=>"d", "b[2]"=>"e"}

2.4.2 :022 > {:a => "a", :b => ["c", "d", "e"]}.to_query
 => "a=a&b%5B0%5D=c&b%5B1%5D=d&b%5B2%5D=e"

答案 15 :(得分:0)

2.6.3 :001 > hash = {:a => "a", :b => ["c", "d", "e"]}
=> {:a=>"a", :b=>["c", "d", "e"]}
2.6.3 :002 > hash.to_a.map { |x| "#{x[0]}=#{x[1].class == Array ? x[1].join(",") : x[1]}" 
}.join("&")
=> "a=a&b=c,d,e"