summaryrefslogtreecommitdiff
path: root/lib/net
diff options
context:
space:
mode:
authoraamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-05-06 09:43:26 +0000
committeraamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-05-06 09:43:26 +0000
commit9f1c215f5a8e504cd87a738869e4a1701ee3749d (patch)
treecf8eb61543abddcd974dee5f4553e6c4601ff1e5 /lib/net
parent73e20674f62565116af5e9d91264efd351f68477 (diff)
* lib/net/http.rb (Net::HTTP.post_form): allow an Array of String for pairs argument. [ruby-Bugs:10340]
* lib/net/http.rb (Net::HTTP#set_form_data): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12250 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/net')
-rw-r--r--lib/net/http.rb25
1 files changed, 21 insertions, 4 deletions
diff --git a/lib/net/http.rb b/lib/net/http.rb
index 7757c7b383..060753e62e 100644
--- a/lib/net/http.rb
+++ b/lib/net/http.rb
@@ -86,19 +86,20 @@ module Net #:nodoc:
#
# #1: Simple POST
# res = Net::HTTP.post_form(URI.parse('http://www.example.com/search.cgi'),
- # {'q'=>'ruby', 'max'=>'50'})
+ # {'q' => 'ruby', 'max' => '50'})
# puts res.body
#
# #2: POST with basic authentication
# res = Net::HTTP.post_form(URI.parse('http://jack:pass@www.example.com/todo.cgi'),
- # {'from'=>'2005-01-01', 'to'=>'2005-03-31'})
+ # {'from' => '2005-01-01',
+ # 'to' => '2005-03-31'})
# puts res.body
#
# #3: Detailed control
# url = URI.parse('http://www.example.com/todo.cgi')
# req = Net::HTTP::Post.new(url.path)
# req.basic_auth 'jack', 'pass'
- # req.set_form_data({'from'=>'2005-01-01', 'to'=>'2005-03-31'}, ';')
+ # req.set_form_data({'from' => '2005-01-01', 'to' => '2005-03-31'}, ';')
# res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
# case res
# when Net::HTTPSuccess, Net::HTTPRedirection
@@ -106,6 +107,11 @@ module Net #:nodoc:
# else
# res.error!
# end
+ #
+ # #4: Multiple values
+ # res = Net::HTTP.post_form(URI.parse('http://www.example.com/search.cgi'),
+ # {'q' => ['ruby', 'perl'], 'max' => '50'})
+ # puts res.body
#
# === Accessing via Proxy
#
@@ -1445,13 +1451,24 @@ module Net #:nodoc:
#
# This method also set Content-Type: header field to
# application/x-www-form-urlencoded.
+ #
+ # Example:
+ # http.form_data = {"q" => "ruby", "lang" => "en"}
+ # http.form_data = {"q" => ["ruby", "perl"], "lang" => "en"}
+ # http.set_form_data({"q" => "ruby", "lang" => "en"}, ';')
+ #
def set_form_data(params, sep = '&')
- self.body = params.map {|k,v| "#{urlencode(k.to_s)}=#{urlencode(v.to_s)}" }.join(sep)
+ self.body = params.map {|k, v| encode_kvpair(k, v) }.flatten.join(sep)
self.content_type = 'application/x-www-form-urlencoded'
end
alias form_data= set_form_data
+ def encode_kvpair(k, vs)
+ Array(vs).map {|v| "#{urlencode(k)}=#{urlencode(v.to_s)}" }
+ end
+ private :encode_kvpair
+
def urlencode(str)
str.gsub(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x', s[0]) }
end