Skip to content
On this page

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

class · Ruby 3.2

Net::HTTPResponse

< Object include Net::HTTPHeader 6 instance methods 4 class methods

This class is the base class for Net::HTTP request classes.

About the Examples

Examples here assume that net/http has been required (which also requires uri):

Example
require 'net/http'

Many code examples here use these example websites:

Some examples also assume these variables:

Example
uri = URI('https://jsonplaceholder.typicode.com')
uri.freeze # Examples may not modify.
hostname = uri.hostname # => "jsonplaceholder.typicode.com"
port = uri.port         # => 443

So that example requests may be written as:

Example
Net::HTTP.get(uri)
Net::HTTP.get(hostname, '/index.html')
Net::HTTP.start(hostname) do |http|
  http.get('/todos/1')
  http.get('/todos/2')
end

An example that needs a modified URI first duplicates uri, then modifies the duplicate:

Example
_uri = uri.dup
_uri.path = '/todos/1'

Returned Responses

Method Net::HTTP.get_response returns an instance of one of the subclasses of Net::HTTPResponse:

Example
Net::HTTP.get_response(uri)
# => #<Net::HTTPOK 200 OK readbody=true>
Net::HTTP.get_response(hostname, '/nosuch')
# => #<Net::HTTPNotFound 404 Not Found readbody=true>

As does method Net::HTTP#request:

Example
req = Net::HTTP::Get.new(uri)
Net::HTTP.start(hostname) do |http|
  http.request(req)
end # => #<Net::HTTPOK 200 OK readbody=true>

Class Net::HTTPResponse includes module Net::HTTPHeader, which provides access to response header values via (among others):

  • Hash-like method [].

  • Specific reader methods, such as content_type.

Examples:

Example
res = Net::HTTP.get_response(uri) # => #<Net::HTTPOK 200 OK readbody=true>
res['Content-Type']               # => "text/html; charset=UTF-8"
res.content_type                  # => "text/html"

Response Subclasses

Class Net::HTTPResponse has a subclass for each HTTP status code. You can look up the response class for a given code:

Example
Net::HTTPResponse::CODE_TO_OBJ['200'] # => Net::HTTPOK
Net::HTTPResponse::CODE_TO_OBJ['400'] # => Net::HTTPBadRequest
Net::HTTPResponse::CODE_TO_OBJ['404'] # => Net::HTTPNotFound

And you can retrieve the status code for a response object:

Example
Net::HTTP.get_response(uri).code                 # => "200"
Net::HTTP.get_response(hostname, '/nosuch').code # => "404"

The response subclasses (indentation shows class hierarchy):

There is also the Net::HTTPBadResponse exception which is raised when there is a protocol error.

Class methods 4

Instance methods 6

Constants
No documentation available
No documentation available
Attributes

The encoding to use for the response body. If Encoding, use that encoding. If other true value, attempt to detect the appropriate encoding, and use that.

Read

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.

Read & Write

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

The HTTP version supported by the server.

Read & Write

Whether to ignore EOF when reading bodies with a specified Content-Length header.

Read

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

uri

Read

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