ruby-changes:44524
From: shugo <ko1@a...>
Date: Sat, 5 Nov 2016 23:39:11 +0900 (JST)
Subject: [ruby-changes:44524] shugo:r56597 (trunk): * lib/net/http.rb (Net::HTTP.post): new convenience method to send a POST request.
shugo 2016-11-05 23:39:06 +0900 (Sat, 05 Nov 2016) New Revision: 56597 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56597 Log: * lib/net/http.rb (Net::HTTP.post): new convenience method to send a POST request. Modified files: trunk/ChangeLog trunk/NEWS trunk/lib/net/http.rb trunk/test/net/http/test_http.rb Index: NEWS =================================================================== --- NEWS (revision 56596) +++ NEWS (revision 56597) @@ -217,6 +217,9 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L217 * Don't allow , as a separator [Bug #12791] +* Net::HTTP + * New method: Net::HTTP.post [Feature #12375] + === Compatibility issues (excluding feature bug fixes) * Array#sum and Enumerable#sum are implemented. [Feature #12217] Index: ChangeLog =================================================================== --- ChangeLog (revision 56596) +++ ChangeLog (revision 56597) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sat Nov 5 23:30:41 2016 Shugo Maeda <shugo@r...> + + * lib/net/http.rb (Net::HTTP.post): new convenience method to send + a POST request. + Sat Nov 5 23:03:54 2016 NARUSE, Yui <naruse@r...> * lib/net/http.rb (transport_request): other than HTTPContinue Index: lib/net/http.rb =================================================================== --- lib/net/http.rb (revision 56596) +++ lib/net/http.rb (revision 56597) @@ -489,6 +489,24 @@ module Net #:nodoc: https://github.com/ruby/ruby/blob/trunk/lib/net/http.rb#L489 end end + # Posts data to the specified URI object. + # + # Example: + # + # require 'net/http' + # require 'uri' + # + # Net::HTTP.post URI('http://www.example.com/api/search'), + # { "q" => "ruby", "max" => "50" }.to_json, + # "Content-Type" => "application/json" + # + def HTTP.post(url, data, header = nil) + start(url.hostname, url.port, + :use_ssl => url.scheme == 'https' ) {|http| + http.post(url.path, data, header) + } + end + # Posts HTML form data to the specified URI object. # The form data must be provided as a Hash mapping from String to String. # Example: Index: test/net/http/test_http.rb =================================================================== --- test/net/http/test_http.rb (revision 56596) +++ test/net/http/test_http.rb (revision 56597) @@ -384,6 +384,22 @@ module TestNetHTTP_version_1_1_methods https://github.com/ruby/ruby/blob/trunk/test/net/http/test_http.rb#L384 end end + def test_s_post + url = "http://#{config('host')}:#{config('port')}/" + res = Net::HTTP.post( + URI.parse(url), + "a=x") + assert_equal "application/x-www-form-urlencoded", res["Content-Type"] + assert_equal "a=x", res.body + + res = Net::HTTP.post( + URI.parse(url), + "hello world", + "Content-Type" => "text/plain; charset=US-ASCII") + assert_equal "text/plain; charset=US-ASCII", res["Content-Type"] + assert_equal "hello world", res.body + end + def test_s_post_form url = "http://#{config('host')}:#{config('port')}/" res = Net::HTTP.post_form( -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/