HTTP response class.

This class wraps together the response header and the response body (the entity requested).

It mixes in the HTTPHeader module, which provides access to response header values both via hash-like methods and via individual readers.

Note that each possible HTTP response code defines its own HTTPResponse subclass. These are listed below.

All classes are defined under the Net module. Indentation indicates inheritance. For a list of the classes see Net::HTTP.

Methods
B
E
I
R
V
Included Modules
Constants
CODE_CLASS_TO_OBJ = { '1' => Net::HTTPInformation, '2' => Net::HTTPSuccess, '3' => Net::HTTPRedirection, '4' => Net::HTTPClientError, '5' => Net::HTTPServerError }
 
CODE_TO_OBJ = { '100' => Net::HTTPContinue, '101' => Net::HTTPSwitchProtocol, '200' => Net::HTTPOK, '201' => Net::HTTPCreated, '202' => Net::HTTPAccepted, '203' => Net::HTTPNonAuthoritativeInformation, '204' => Net::HTTPNoContent, '205' => Net::HTTPResetContent, '206' => Net::HTTPPartialContent, '207' => Net::HTTPMultiStatus, '300' => Net::HTTPMultipleChoices, '301' => Net::HTTPMovedPermanently, '302' => Net::HTTPFound, '303' => Net::HTTPSeeOther, '304' => Net::HTTPNotModified, '305' => Net::HTTPUseProxy, '307' => Net::HTTPTemporaryRedirect, '400' => Net::HTTPBadRequest, '401' => Net::HTTPUnauthorized, '402' => Net::HTTPPaymentRequired, '403' => Net::HTTPForbidden, '404' => Net::HTTPNotFound, '405' => Net::HTTPMethodNotAllowed, '406' => Net::HTTPNotAcceptable, '407' => Net::HTTPProxyAuthenticationRequired, '408' => Net::HTTPRequestTimeOut, '409' => Net::HTTPConflict, '410' => Net::HTTPGone, '411' => Net::HTTPLengthRequired, '412' => Net::HTTPPreconditionFailed, '413' => Net::HTTPRequestEntityTooLarge, '414' => Net::HTTPRequestURITooLong, '415' => Net::HTTPUnsupportedMediaType, '416' => Net::HTTPRequestedRangeNotSatisfiable, '417' => Net::HTTPExpectationFailed, '422' => Net::HTTPUnprocessableEntity, '423' => Net::HTTPLocked, '424' => Net::HTTPFailedDependency, '426' => Net::HTTPUpgradeRequired, '428' => Net::HTTPPreconditionRequired, '429' => Net::HTTPTooManyRequests, '431' => Net::HTTPRequestHeaderFieldsTooLarge, '500' => Net::HTTPInternalServerError, '501' => Net::HTTPNotImplemented, '502' => Net::HTTPBadGateway, '503' => Net::HTTPServiceUnavailable, '504' => Net::HTTPGatewayTimeOut, '505' => Net::HTTPVersionNotSupported, '507' => Net::HTTPInsufficientStorage, '511' => Net::HTTPNetworkAuthenticationRequired, }
 
Attributes
[R] code

The HTTP result code string. For example, '302'. You can also determine the response type by examining which response subclass the response object is an instance of.

[RW] decode_content

Set to true automatically when the request did not contain an Accept-Encoding header from the user.

[R] http_version

The HTTP version supported by the server.

[R] message

The HTTP result message sent by the server. For example, 'Not Found'.

[R] msg

The HTTP result message sent by the server. For example, 'Not Found'.

[R] uri

The URI used to fetch this response. The response URI is only available if a URI was used to create the request.

Instance Public methods
body()

Returns the full entity body.

Calling this method a second or subsequent time will return the string already read.

http.request_get('/index.html') {|res|
  puts res.body
}

http.request_get('/index.html') {|res|
  p res.body.object_id   # 538149362
  p res.body.object_id   # 538149362
}
Also aliased as: entity
# File lib/net/http/response.rb, line 225
def body
  read_body()
end
body=(value)

Because it may be necessary to modify the body, Eg, decompression this method facilitates that.

# File lib/net/http/response.rb, line 231
def body=(value)
  @body = value
end
body_permitted?()

true if the response has a body.

# File lib/net/http/response.rb, line 19
def body_permitted?
  self::HAS_BODY
end
entity()
Alias for: body
inspect()
# File lib/net/http/response.rb, line 106
def inspect
  "#<#{self.class} #{@code} #{@message} readbody=#{@read}>"
end
read_body(dest = nil, &block)

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.

Calling this method a second or subsequent time for the same HTTPResponse object will return the value already read.

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
}
# File lib/net/http/response.rb, line 193
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
value()

Raises an HTTP error if the response is not 2xx (success).

# File lib/net/http/response.rb, line 127
def value
  error! unless self.kind_of?(Net::HTTPSuccess)
end