Class

HTTP

Extends:

What Is This Library?

This library provides your program functions to access WWW documents via HTTP, Hyper Text Transfer Protocol version 1.1. For details of HTTP, refer [RFC2616] (www.ietf.org/rfc/rfc2616.txt).

Examples

Getting Document From WWW Server

Example #1: Simple GET+print

require 'net/http'
Net::HTTP.get_print 'www.example.com', '/index.html'

Example #2: Simple GET+print by URL

require 'net/http'
require 'uri'
Net::HTTP.get_print URI.parse('http://www.example.com/index.html')

Example #3: More generic GET+print

require 'net/http'
require 'uri'

url = URI.parse('http://www.example.com/index.html')
res = Net::HTTP.start(url.host, url.port) {|http|
  http.get('/index.html')
}
puts res.body

Example #4: More generic GET+print

require 'net/http'

url = URI.parse('http://www.example.com/index.html')
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(url.host, url.port) {|http|
  http.request(req)
}
puts res.body

Posting Form Data

require 'net/http'
require 'uri'

#1: Simple POST
res = Net::HTTP.post_form(URI.parse('http://www.example.com/search.cgi'),
                          {'q'=>'ruby', 'max'=>'50'})
puts res.body

#2: POST with basic authentication
res = Net::HTTP.post_form(URI.parse('http://jack:pass@www.example.com/todo.cgi'),
                                    {'from'=>'2005-01-01', 'to'=>'2005-03-31'})
puts res.body

#3: Detailed control
url = URI.parse('http://www.example.com/todo.cgi')
req = Net::HTTP::Post.new(url.path)
req.basic_auth 'jack', 'pass'
req.set_form_data({'from'=>'2005-01-01', 'to'=>'2005-03-31'}, ';')
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
case res
when Net::HTTPSuccess, Net::HTTPRedirection
  # OK
else
  res.error!
end

Accessing via Proxy

Net::HTTP.Proxy creates http proxy class. It has same methods of Net::HTTP but its instances always connect to proxy, instead of given host.

require 'net/http'

proxy_addr = 'your.proxy.host'
proxy_port = 8080
        :
Net::HTTP::Proxy(proxy_addr, proxy_port).start('www.example.com') {|http|
  # always connect to your.proxy.addr:8080
        :
}

Since Net::HTTP.Proxy returns Net::HTTP itself when proxy_addr is nil, there’s no need to change code if there’s proxy or not.

There are two additional parameters in Net::HTTP.Proxy which allow to specify proxy user name and password:

Net::HTTP::Proxy(proxy_addr, proxy_port, proxy_user = nil, proxy_pass = nil)

You may use them to work with authorization-enabled proxies:

require 'net/http'
require 'uri'

proxy_host = 'your.proxy.host'
proxy_port = 8080
uri = URI.parse(ENV['http_proxy'])
proxy_user, proxy_pass = uri.userinfo.split(/:/) if uri.userinfo
Net::HTTP::Proxy(proxy_host, proxy_port,
                 proxy_user, proxy_pass).start('www.example.com') {|http|
  # always connect to your.proxy.addr:8080 using specified username and password
        :
}

Note that net/http never rely on HTTP_PROXY environment variable. If you want to use proxy, set it explicitly.

Following Redirection

require 'net/http'
require 'uri'

def fetch(uri_str, limit = 10)
  # You should choose better exception.
  raise ArgumentError, 'HTTP redirect too deep' if limit == 0

  response = Net::HTTP.get_response(URI.parse(uri_str))
  case response
  when Net::HTTPSuccess     then response
  when Net::HTTPRedirection then fetch(response['location'], limit - 1)
  else
    response.error!
  end
end

print fetch('http://www.ruby-lang.org')

Net::HTTPSuccess and Net::HTTPRedirection is a HTTPResponse class. All HTTPResponse objects belong to its own response class which indicate HTTP result status. For details of response classes, see section "HTTP Response Classes".

Basic Authentication

require 'net/http'

Net::HTTP.start('www.example.com') {|http|
  req = Net::HTTP::Get.new('/secret-page.html')
  req.basic_auth 'account', 'password'
  response = http.request(req)
  print response.body
}

HTTP Request Classes

Here is HTTP request class hierarchy.

Net::HTTPRequest
    Net::HTTP::Get
    Net::HTTP::Head
    Net::HTTP::Post
    Net::HTTP::Put
    Net::HTTP::Proppatch
    Net::HTTP::Lock
    Net::HTTP::Unlock
    Net::HTTP::Options
    Net::HTTP::Propfind
    Net::HTTP::Delete
    Net::HTTP::Move
    Net::HTTP::Copy
    Net::HTTP::Mkcol
    Net::HTTP::Trace

HTTP Response Classes

Here is HTTP response class hierarchy. All classes are defined in Net module.

HTTPResponse
    HTTPUnknownResponse
    HTTPInformation                    # 1xx
        HTTPContinue                       # 100
        HTTPSwitchProtocl                  # 101
    HTTPSuccess                        # 2xx
        HTTPOK                             # 200
        HTTPCreated                        # 201
        HTTPAccepted                       # 202
        HTTPNonAuthoritativeInformation    # 203
        HTTPNoContent                      # 204
        HTTPResetContent                   # 205
        HTTPPartialContent                 # 206
    HTTPRedirection                    # 3xx
        HTTPMultipleChoice                 # 300
        HTTPMovedPermanently               # 301
        HTTPFound                          # 302
        HTTPSeeOther                       # 303
        HTTPNotModified                    # 304
        HTTPUseProxy                       # 305
        HTTPTemporaryRedirect              # 307
    HTTPClientError                    # 4xx
        HTTPBadRequest                     # 400
        HTTPUnauthorized                   # 401
        HTTPPaymentRequired                # 402
        HTTPForbidden                      # 403
        HTTPNotFound                       # 404
        HTTPMethodNotAllowed               # 405
        HTTPNotAcceptable                  # 406
        HTTPProxyAuthenticationRequired    # 407
        HTTPRequestTimeOut                 # 408
        HTTPConflict                       # 409
        HTTPGone                           # 410
        HTTPLengthRequired                 # 411
        HTTPPreconditionFailed             # 412
        HTTPRequestEntityTooLarge          # 413
        HTTPRequestURITooLong              # 414
        HTTPUnsupportedMediaType           # 415
        HTTPRequestedRangeNotSatisfiable   # 416
        HTTPExpectationFailed              # 417
    HTTPServerError                    # 5xx
        HTTPInternalServerError            # 500
        HTTPNotImplemented                 # 501
        HTTPBadGateway                     # 502
        HTTPServiceUnavailable             # 503
        HTTPGatewayTimeOut                 # 504
        HTTPVersionNotSupported            # 505

Switching Net::HTTP versions

You can use net/http.rb 1.1 features (bundled with Ruby 1.6) by calling HTTP.version_1_1. Calling Net::HTTP.version_1_2 allows you to use 1.2 features again.

# example
Net::HTTP.start {|http1| ...(http1 has 1.2 features)... }

Net::HTTP.version_1_1
Net::HTTP.start {|http2| ...(http2 has 1.1 features)... }

Net::HTTP.version_1_2
Net::HTTP.start {|http3| ...(http3 has 1.2 features)... }

This function is NOT thread-safe.

Modules
ProxyDelta
Classes
Copy
Delete
Get HTTP 1.1 methods — RFC2616
Head
Lock
Mkcol
Move
Options
Post
Propfind WebDAV methods — RFC2518
Proppatch
Put
Trace
Unlock
Aliases
is_version_1_1?
is_version_1_2?
newobj
Public Attributes
address The host name to connect to.
close_on_empty_response
open_timeout Seconds to wait until connection is opened. If the HTTP object cannot open a connection in this many seconds, it raises a TimeoutError exception.
port The port number to connect to.
proxy_address
proxy_pass
proxy_port
proxy_user
read_timeout Seconds to wait until reading one block (by one read(2) call). If the HTTP object cannot open a connection in this many seconds, it raises a TimeoutError exception.
Public Methods
Proxy Creates an HTTP proxy class. Arguments are address/port of proxy host and username/password if authorization on proxy server is required. You can replace the HTTP class with created proxy class.
active? Alias for #started?
copy Sends a COPY request to the path and gets a response, as an HTTPResponse object.
default_port The default port to use for HTTP requests; defaults to 80.
delete Sends a DELETE request to the path and gets a response, as an HTTPResponse object.
finish Finishes HTTP session and closes TCP connection. Raises IOError if not started.
get Gets data from path on the connected-to host. header must be a Hash like { ‘Accept’ => ’*/*’, … }.
get Send a GET request to the target and return the response as a string. The target can either be specified as (uri), or as (host, path, port = 80); so:
get2 Alias for #request_get
get_print Get body from target and output it to +$stdout+. The target can either be specified as (uri), or as (host, path, port = 80); so:
get_response Send a GET request to the target and return the response as a Net::HTTPResponse object. The target can either be specified as (uri), or as (host, path, port = 80); so:
head Gets only the header from path on the connected-to host. header is a Hash like { ‘Accept’ => ’*/*’, … }.
head2 Alias for #request_head
http_default_port The default port to use for HTTP requests; defaults to 80.
https_default_port The default port to use for HTTPS requests; defaults to 443.
inspect
lock Sends a LOCK request to the path and gets a response, as an HTTPResponse object.
mkcol Sends a MKCOL request to the path and gets a response, as an HTTPResponse object.
move Sends a MOVE request to the path and gets a response, as an HTTPResponse object.
new Creates a new Net::HTTP object. If proxy_addr is given, creates an Net::HTTP object with proxy support. This method does not open the TCP connection.
new Creates a new Net::HTTP object for the specified address. This method does not open the TCP connection.
options Sends a OPTIONS request to the path and gets a response, as an HTTPResponse object.
peer_cert
post Posts data (must be a String) to path. header must be a Hash like { ‘Accept’ => ’*/*’, … }.
post2 Alias for #request_post
post_form Posts HTML form data to the URL. Form data must be represented as a Hash of String to String, e.g:
propfind Sends a PROPFIND request to the path and gets a response, as an HTTPResponse object.
proppatch Sends a PROPPATCH request to the path and gets a response, as an HTTPResponse object.
proxy? True if self is a HTTP proxy class.
proxy_address Address of proxy host. If self does not use a proxy, nil.
proxy_class? returns true if self is a class which was created by HTTP::Proxy.
proxy_pass User password for accessing proxy. If self does not use a proxy, nil.
proxy_port Port number of proxy host. If self does not use a proxy, nil.
proxy_user User name for accessing proxy. If self does not use a proxy, nil.
proxyaddr Alias for #proxy_address
proxyport Alias for #proxy_port
put
put2 Alias for #request_put
read_timeout= Setter for the read_timeout attribute.
request Sends an HTTPRequest object REQUEST to the HTTP server. This method also sends DATA string if REQUEST is a post/put request. Giving DATA for get/head request causes ArgumentError.
request_get Sends a GET request to the path and gets a response, as an HTTPResponse object.
request_head Sends a HEAD request to the path and gets a response, as an HTTPResponse object.
request_post Sends a POST request to the path and gets a response, as an HTTPResponse object.
request_put
send_request Sends an HTTP request to the HTTP server. This method also sends DATA string if DATA is given.
set_debug_output WARNING This method causes serious security hole. Never use this method in production code.
socket_type
ssl_context_accessor
ssl_timeout
ssl_timeout=
start creates a new Net::HTTP object and opens its TCP connection and HTTP session. If the optional block is given, the newly created Net::HTTP object is passed to it and closed when the block finishes. In this case, the return value of this method is the return value of the block. If no block is given, the return value of this method is the newly created Net::HTTP object itself, and the caller is responsible for closing it upon completion.
start Opens TCP connection and HTTP session.
started? returns true if the HTTP session is started.
timeout= Alias for #ssl_timeout=
trace Sends a TRACE request to the path and gets a response, as an HTTPResponse object.
unlock Sends a UNLOCK request to the path and gets a response, as an HTTPResponse object.
use_ssl Alias for #use_ssl?
use_ssl= Turn on/off SSL. This flag must be set before starting session. If you change use_ssl value after session started, a Net::HTTP object raises IOError.
use_ssl? returns true if use SSL/TLS with HTTP.
use_ssl?
version_1_1 Turns on net/http 1.1 (ruby 1.6) features. Defaults to OFF in ruby 1.8.
version_1_1? true if net/http is in version 1.1 compatible mode. Defaults to true.
version_1_2 Turns on net/http 1.2 (ruby 1.8) features. Defaults to ON in ruby 1.8.
version_1_2? true if net/http is in version 1.2 mode. Defaults to true.
Private Methods
D
addr_port utils
begin_transport
conn_address without proxy
conn_port
connect
do_finish
do_start
edit_path
end_transport
keep_alive?
on_connect
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.