* 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
This commit is contained in:
parent
73e20674f6
commit
9f1c215f5a
@ -1,3 +1,10 @@
|
|||||||
|
Sun May 6 18:44:11 2007 Minero Aoki <aamine@loveruby.net>
|
||||||
|
|
||||||
|
* 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.
|
||||||
|
|
||||||
Sun May 6 17:54:36 2007 Minero Aoki <aamine@loveruby.net>
|
Sun May 6 17:54:36 2007 Minero Aoki <aamine@loveruby.net>
|
||||||
|
|
||||||
* lib/net/http.rb: Connection header field might include both of
|
* lib/net/http.rb: Connection header field might include both of
|
||||||
|
@ -86,19 +86,20 @@ module Net #:nodoc:
|
|||||||
#
|
#
|
||||||
# #1: Simple POST
|
# #1: Simple POST
|
||||||
# res = Net::HTTP.post_form(URI.parse('http://www.example.com/search.cgi'),
|
# res = Net::HTTP.post_form(URI.parse('http://www.example.com/search.cgi'),
|
||||||
# {'q'=>'ruby', 'max'=>'50'})
|
# {'q' => 'ruby', 'max' => '50'})
|
||||||
# puts res.body
|
# puts res.body
|
||||||
#
|
#
|
||||||
# #2: POST with basic authentication
|
# #2: POST with basic authentication
|
||||||
# res = Net::HTTP.post_form(URI.parse('http://jack:pass@www.example.com/todo.cgi'),
|
# 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
|
# puts res.body
|
||||||
#
|
#
|
||||||
# #3: Detailed control
|
# #3: Detailed control
|
||||||
# url = URI.parse('http://www.example.com/todo.cgi')
|
# url = URI.parse('http://www.example.com/todo.cgi')
|
||||||
# req = Net::HTTP::Post.new(url.path)
|
# req = Net::HTTP::Post.new(url.path)
|
||||||
# req.basic_auth 'jack', 'pass'
|
# 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) }
|
# res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
|
||||||
# case res
|
# case res
|
||||||
# when Net::HTTPSuccess, Net::HTTPRedirection
|
# when Net::HTTPSuccess, Net::HTTPRedirection
|
||||||
@ -107,6 +108,11 @@ module Net #:nodoc:
|
|||||||
# res.error!
|
# res.error!
|
||||||
# end
|
# 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
|
# === Accessing via Proxy
|
||||||
#
|
#
|
||||||
# Net::HTTP.Proxy creates http proxy class. It has same
|
# Net::HTTP.Proxy creates http proxy class. It has same
|
||||||
@ -1445,13 +1451,24 @@ module Net #:nodoc:
|
|||||||
#
|
#
|
||||||
# This method also set Content-Type: header field to
|
# This method also set Content-Type: header field to
|
||||||
# application/x-www-form-urlencoded.
|
# 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 = '&')
|
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'
|
self.content_type = 'application/x-www-form-urlencoded'
|
||||||
end
|
end
|
||||||
|
|
||||||
alias form_data= set_form_data
|
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)
|
def urlencode(str)
|
||||||
str.gsub(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x', s[0]) }
|
str.gsub(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x', s[0]) }
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user