diff --git a/extras/sponge.rb b/extras/sponge.rb index 24a646bd8..864482bbe 100644 --- a/extras/sponge.rb +++ b/extras/sponge.rb @@ -180,7 +180,7 @@ def fetch(url, method = :get, fields = nil, raw_post_data = nil, headers = {}, l post_data = raw_post_data send_headers["Content-Type"] = "application/x-www-form-urlencoded" else - post_data = fields.map { |k, v| "#{k}=#{v}" }.join("&") + post_data = encode_fields(fields) end send_headers["Content-Length"] = post_data.length.to_s @@ -263,4 +263,26 @@ def dputs(string) puts string end end + + def encode_fields(fields) + fields.map { |k, v| + if v.is_a?(Hash) + # :user => { :name => "hi", :age => "1" } + # becomes + # user[hame]=hi and user[age]=1 + v.map { |vk, vv| + [ CGI.escape("#{k}[#{vk}]"), CGI.escape(vv.to_s) ].join("=") + } + elsif v.is_a?(Array) + # :user => [ "one", "two" ] + # becomes + # user[]=one and user[]=two + v.map{|vv| + [ CGI.escape("#{k}[]"), CGI.escape(vv.to_s) ].join("=") + } + else + [ CGI.escape(k.to_s), CGI.escape(v.to_s) ].join("=") + end + }.flatten.join("&") + end end