Class: Net::HTTP::HTTP
| Relationships & Source Files | |
| Inherits: | Object |
| Defined in: | lib/net/http.rb |
Class Attribute Summary
-
.default_configuration
rw
Allows to set the default configuration that will be used when creating a new connection.
-
.proxy_address
readonly
Returns the address of the proxy host, or
nilif none; see Net::HTTP@Proxy+Server. -
.proxy_class? ⇒ Boolean
readonly
Returns true if self is a class which was created by
HTTP::Proxy. -
.proxy_pass
readonly
Returns the password for accessing the proxy, or
nilif none; see Net::HTTP@Proxy+Server. -
.proxy_port
readonly
Returns the port number of the proxy host, or
nilif none; see Net::HTTP@Proxy+Server. -
.proxy_use_ssl
readonly
Use SSL when talking to the proxy.
-
.proxy_user
readonly
Returns the user name for accessing the proxy, or
nilif none; see Net::HTTP@Proxy+Server.
Class Method Summary
-
.default_port
Returns integer
80, the default port to use for HTTP requests: -
Net::HTTP.get(hostname, path, port = 80) ⇒ body
Sends a GET request and returns the HTTP response body as a string.
-
Net::HTTP.get_print(hostname, path, port = 80) ⇒ nil
Like
Net::HTTP.get, but writes the returned body to $stdout; returnsnil. -
Net::HTTP.get_response(hostname, path, port = 80) ⇒ HTTP
Like
Net::HTTP.get, but returns a::Net::HTTPResponseobject instead of the body string. -
.http_default_port
Returns integer
80, the default port to use for HTTP requests: -
.https_default_port
Returns integer
443, the default port to use for HTTPS requests: -
.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, p_no_proxy = nil, p_use_ssl = nil) ⇒ HTTP
constructor
Returns a new Net::HTTP object
http(but does not open a TCP connection or HTTP session). -
.post(url, data, header = nil)
Posts data to a host; returns a
::Net::HTTPResponseobject. -
.post_form(url, params)
Posts data to a host; returns a
::Net::HTTPResponseobject. -
.put(url, data, header = nil)
Sends a PUT request to the server; returns a
::Net::HTTPResponseobject. -
.start(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, opts) ⇒ HTTP
Creates a new Net::HTTP object,
http, via Net::HTTP.new: - .is_version_1_1? Internal use only
- .is_version_1_2? Internal use only
- .newobj Internal use only
-
Proxy(p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, p_use_ssl = nil)
Internal use only
Creates an HTTP proxy class which behaves like Net::HTTP, but performs all access via the specified proxy.
-
.socket_type
Internal use only
obsolete.
Constructor Details
.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, p_no_proxy = nil, p_use_ssl = nil) ⇒ HTTP
Returns a new Net::HTTP object http
(but does not open a TCP connection or HTTP session).
With only string argument Net::HTTP#address given
(and ENV undefined or nil),
the returned http:
- Has the given address.
- Has the default port number,
Net::HTTP.default_port(80). - Has no proxy.
Example:
http = Net::HTTP.new(hostname)
=> #<Net::HTTP jsonplaceholder.typicode.com:80 open=false>
http.address # => "jsonplaceholder.typicode.com" http.port # => 80 http.proxy? # => false
With integer argument Net::HTTP#port also given,
the returned http has the given port:
http = Net::HTTP.new(hostname, 8000)
=> #<Net::HTTP jsonplaceholder.typicode.com:8000 open=false>
http.port # => 8000
For proxy-defining arguments p_addr through p_no_proxy,
see {Net::HTTP::HTTP.Proxy Server}.
# File 'lib/net/http.rb', line 1100
def HTTP.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, p_no_proxy = nil, p_use_ssl = nil) http = super address, port if proxy_class? then # from Net::HTTP::Proxy() http.proxy_from_env = @proxy_from_env http.proxy_address = @proxy_address http.proxy_port = @proxy_port http.proxy_user = @proxy_user http.proxy_pass = @proxy_pass http.proxy_use_ssl = @proxy_use_ssl elsif p_addr == :ENV then http.proxy_from_env = true else if p_addr && p_no_proxy && !URI::Generic.use_proxy?(address, address, port, p_no_proxy) p_addr = nil p_port = nil end http.proxy_address = p_addr http.proxy_port = p_port || default_port http.proxy_user = p_user http.proxy_pass = p_pass http.proxy_use_ssl = p_use_ssl end http end
Class Attribute Details
.default_configuration (rw)
Allows to set the default configuration that will be used when creating a new connection.
Example:
Net::HTTP.default_configuration = {
read_timeout: 1,
write_timeout: 1
}
http = Net::HTTP.new(hostname) http.open_timeout # => 60 http.read_timeout # => 1 http.write_timeout # => 1
# File 'lib/net/http.rb', line 1142
attr_accessor :default_configuration
.proxy_address (readonly)
Returns the address of the proxy host, or nil if none;
see Net::HTTP@Proxy+Server.
# File 'lib/net/http.rb', line 1861
attr_reader :proxy_address
.proxy_class? ⇒ Boolean (readonly)
Returns true if self is a class which was created by HTTP::Proxy.
# File 'lib/net/http.rb', line 1855
def proxy_class? defined?(@is_proxy_class) ? @is_proxy_class : false end
.proxy_pass (readonly)
Returns the password for accessing the proxy, or nil if none;
see Net::HTTP@Proxy+Server.
# File 'lib/net/http.rb', line 1873
attr_reader :proxy_pass
.proxy_port (readonly)
Returns the port number of the proxy host, or nil if none;
see Net::HTTP@Proxy+Server.
# File 'lib/net/http.rb', line 1865
attr_reader :proxy_port
.proxy_use_ssl (readonly)
Use SSL when talking to the proxy. If ::Net::HTTP does not use a proxy, nil.
# File 'lib/net/http.rb', line 1876
attr_reader :proxy_use_ssl
.proxy_user (readonly)
Returns the user name for accessing the proxy, or nil if none;
see Net::HTTP@Proxy+Server.
# File 'lib/net/http.rb', line 1869
attr_reader :proxy_user
Class Method Details
.default_port
[ GitHub ]# File 'lib/net/http.rb', line 935
def HTTP.default_port http_default_port() end
Net::HTTP.get(hostname, path, port = 80) ⇒ body
Net::HTTP ⇒ body
body
Net::HTTP ⇒ body
Sends a GET request and returns the HTTP response body as a string.
With string arguments hostname and path:
hostname = 'jsonplaceholder.typicode.com'
path = '/todos/1'
puts Net::HTTP.get(hostname, path)
Output:
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
With URI object uri and optional hash argument headers:
uri = URI('https://jsonplaceholder.typicode.com/todos/1')
headers = {'Content-type' => 'application/json; charset=UTF-8'}
Net::HTTP.get(uri, headers)
Related:
Get: request class for HTTP methodGET.- Net::HTTP#get: convenience method for HTTP method
GET.
# File 'lib/net/http.rb', line 804
def HTTP.get(uri_or_host, path_or_headers = nil, port = nil) get_response(uri_or_host, path_or_headers, port).body end
Net::HTTP.get_print(hostname, path, port = 80) ⇒ nil
Net::HTTP ⇒ nil
nil
Net::HTTP ⇒ nil
Like Net::HTTP.get, but writes the returned body to $stdout;
returns nil.
# File 'lib/net/http.rb', line 763
def HTTP.get_print(uri_or_host, path_or_headers = nil, port = nil) get_response(uri_or_host, path_or_headers, port) {|res| res.read_body do |chunk| $stdout.print chunk end } nil end
Net::HTTP.get_response(hostname, path, port = 80) ⇒ HTTP
Net::HTTP ⇒ HTTP
HTTP
Net::HTTP ⇒ HTTP
Like Net::HTTP.get, but returns a ::Net::HTTPResponse object
instead of the body string.
# File 'lib/net/http.rb', line 814
def HTTP.get_response(uri_or_host, path_or_headers = nil, port = nil, &block) if path_or_headers && !path_or_headers.is_a?(Hash) host = uri_or_host path = path_or_headers new(host, port || HTTP.default_port).start {|http| return http.request_get(path, &block) } else uri = uri_or_host headers = path_or_headers start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') {|http| return http.request_get(uri, headers, &block) } end end
.http_default_port
[ GitHub ]# File 'lib/net/http.rb', line 943
def HTTP.http_default_port 80 end
.https_default_port
# File 'lib/net/http.rb', line 951
def HTTP.https_default_port 443 end
.is_version_1_1?
# File 'lib/net/http.rb', line 753
alias is_version_1_1? version_1_1? #:nodoc:
.is_version_1_2?
# File 'lib/net/http.rb', line 754
alias is_version_1_2? version_1_2? #:nodoc:
.newobj
# File 'lib/net/http.rb', line 1068
alias newobj new # :nodoc:
.post(url, data, header = nil)
Posts data to a host; returns a ::Net::HTTPResponse object.
Argument url must be a URL;
argument data must be a string:
_uri = uri.dup
_uri.path = '/posts'
data = '{"title": "foo", "body": "bar", "userId": 1}'
headers = {'content-type': 'application/json'}
res = Net::HTTP.post(_uri, data, headers) # => #<Net::HTTPCreated 201 Created readbody=true>
puts res.body
Output:
{
"title": "foo",
"body": "bar",
"userId": 1,
"id": 101
}
Related:
Post: request class for HTTP methodPOST.- Net::HTTP#post: convenience method for HTTP method
POST.
.post_form(url, params)
Posts data to a host; returns a ::Net::HTTPResponse object.
Argument url must be a URI;
argument data must be a hash:
_uri = uri.dup
_uri.path = '/posts'
data = {title: 'foo', body: 'bar', userId: 1}
res = Net::HTTP.post_form(_uri, data) # => #<Net::HTTPCreated 201 Created readbody=true>
puts res.body
Output:
{
"title": "foo",
"body": "bar",
"userId": "1",
"id": 101
}
Proxy(p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, p_use_ssl = nil)
Creates an HTTP proxy class which behaves like Net::HTTP, but performs all access via the specified proxy.
This class is obsolete. You may pass these same parameters directly to Net::HTTP.new. See Net::HTTP.new for details of the arguments.
# File 'lib/net/http.rb', line 1829
def HTTP.Proxy(p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, p_use_ssl = nil) #:nodoc: return self unless p_addr Class.new(self) { @is_proxy_class = true if p_addr == :ENV then @proxy_from_env = true @proxy_address = nil @proxy_port = nil else @proxy_from_env = false @proxy_address = p_addr @proxy_port = p_port || default_port end @proxy_user = p_user @proxy_pass = p_pass @proxy_use_ssl = p_use_ssl } end
.put(url, data, header = nil)
Sends a PUT request to the server; returns a ::Net::HTTPResponse object.
Argument url must be a URL;
argument data must be a string:
_uri = uri.dup
_uri.path = '/posts'
data = '{"title": "foo", "body": "bar", "userId": 1}'
headers = {'content-type': 'application/json'}
res = Net::HTTP.put(_uri, data, headers) # => #<Net::HTTPCreated 201 Created readbody=true>
puts res.body
Output:
{
"title": "foo",
"body": "bar",
"userId": 1,
"id": 101
}
Related:
Put: request class for HTTP methodPUT.- Net::HTTP#put: convenience method for HTTP method
PUT.
.socket_type
obsolete
# File 'lib/net/http.rb', line 955
def HTTP.socket_type #:nodoc: obsolete BufferedIO end
.start(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, opts) ⇒ HTTP
.start(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, opts) {|http| ... } ⇒ Object
HTTP
.start(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, opts) {|http| ... } ⇒ Object
Creates a new Net::HTTP object, http, via Net::HTTP.new:
- For arguments Net::HTTP#address and Net::HTTP#port, see Net::HTTP.new.
- For proxy-defining arguments
p_addrthroughp_pass, see Proxy Server. - For argument
opts, see below.
With no block given:
- Calls http.start with no block (see Net::HTTP#start), which opens a TCP connection and HTTP session.
- Returns
http. - The caller should call Net::HTTP#finish to close the session:
http = Net::HTTP.start(hostname)
http.started? # => true
http.finish
http.started? # => false
With a block given:
-
Calls http.start with the block (see Net::HTTP#start), which:
-
Opens a TCP connection and HTTP session.
-
Calls the block, which may make any number of requests to the host.
-
Closes the HTTP session and TCP connection on block exit.
-
Returns the block's value
object. -
Returns
object.
Example:
hostname = 'jsonplaceholder.typicode.com'
Net::HTTP.start(hostname) do |http|
puts http.get('/todos/1').body
puts http.get('/todos/2').body
end
Output:
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
{
"userId": 1,
"id": 2,
"title": "quis ut nam facilis et officia qui",
"completed": false
}
If the last argument given is a hash, it is the opts hash,
where each key is a method or accessor to be called,
and its value is the value to be set.
The keys may include:
- Net::HTTP#ca_file
- Net::HTTP#ca_path
- Net::HTTP#cert
- Net::HTTP#cert_store
- Net::HTTP#ciphers
- Net::HTTP#close_on_empty_response
- Net::HTTP#ipaddr (calls Net::HTTP#ipaddr=)
- Net::HTTP#keep_alive_timeout
- Net::HTTP#key
- Net::HTTP#open_timeout
- Net::HTTP#read_timeout
- Net::HTTP#ssl_timeout
- Net::HTTP#ssl_version
use_ssl(calls Net::HTTP#use_ssl=)- Net::HTTP#verify_callback
- Net::HTTP#verify_depth
- Net::HTTP#verify_mode
- Net::HTTP#write_timeout
Note: If Net::HTTP#port is nil and opts is a truthy value,
the value passed to .new is Net::HTTP.https_default_port, not Net::HTTP#port.
# File 'lib/net/http.rb', line 1045
def HTTP.start(address, *arg, &block) # :yield: http arg.pop if opt = Hash.try_convert(arg[-1]) port, p_addr, p_port, p_user, p_pass = *arg p_addr = :ENV if arg.size < 2 port = https_default_port if !port && opt && opt[:use_ssl] http = new(address, port, p_addr, p_port, p_user, p_pass) http.ipaddr = opt[:ipaddr] if opt && opt[:ipaddr] if opt if opt[:use_ssl] opt = {verify_mode: OpenSSL::SSL::VERIFY_PEER}.update(opt) end http.methods.grep(/\A(\w+)=\z/) do |meth| key = $1.to_sym opt.key?(key) or next http.__send__(meth, opt[key]) end end http.start(&block) end