Method: Net::HTTP#post
- Defined in:
- lib/net/http.rb
#post(path, data, initheader = nil, dest = nil, &block) ⇒ Object
Posts data (must be a String) to path. header must be a Hash like { ‘Accept’ => ‘/’, … }.
In version 1.1 (ruby 1.6), this method returns a pair of objects, a Net::HTTPResponse object and an entity body string. In version 1.2 (ruby 1.8), this method returns a Net::HTTPResponse object.
If called with a block, yields each fragment of the entity body in turn as a string as it are read from the socket. Note that in this case, the returned response object will not contain a (meaningful) body.
dest argument is obsolete. It still works but you must not use it.
In version 1.1, this method might raise an exception for 3xx (redirect). In this case you can get an HTTPResponse object by “anException.response”. In version 1.2, this method never raises exception.
# version 1.1
response, body = http.post('/cgi-bin/search.rb', 'query=foo')
# version 1.2
response = http.post('/cgi-bin/search.rb', 'query=foo')
# using block
File.open('result.txt', 'w') {|f|
http.post('/cgi-bin/search.rb', 'query=foo') do |str|
f.write str
end
}
You should set Content-Type: header field for POST. If no Content-Type: field given, this method uses “application/x-www-form-urlencoded” by default.
843 844 845 846 847 848 849 850 851 852 853 854 |
# File 'lib/net/http.rb', line 843 def post(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segment+ res = nil request(Post.new(path, initheader), data) {|r| r.read_body dest, &block res = r } unless @newimpl res.value return res, res.body end res end |