End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Net::HTTPResponse#read_body
read_body(dest = nil, &block)
lib/net/http/response.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.0.5/lib/net/http/response.rb, line 202
def read_body(dest = nil, &block)
if @read
raise IOError, "#{self.class}\#read_body called twice" if dest or block
return @body
end
to = procdest(dest, block)
stream_check
if @body_exist
read_body_0 to
@body = to
else
@body = nil
end
@read = true
@body
end
Gets the entity body returned by the remote HTTP server.
If a block is given, the body is passed to the block, and the body is provided in fragments, as it is read in from the socket.
If dest argument is given, response is read into that variable, with dest#<< method (it could be String or IO, or any other object responding to <<).
Calling this method a second or subsequent time for the same HTTPResponse object will return the value already read.
Example
http.request_get('/index.html') {|res| puts res.read_body } http.request_get('/index.html') {|res| p res.read_body.object_id # 538149362 p res.read_body.object_id # 538149362 } # using iterator http.request_get('/index.html') {|res| res.read_body do |segment| print segment end }