Skip to content

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

class method

Net::HTTP.new

new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, p_no_proxy = nil)

Returns a new Net::HTTP object http (but does not open a TCP connection or HTTP session).

No Proxy

With only string argument hostname given (and ENV['http_proxy'] undefined or nil), the returned http:

Example:

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 port also given, the returned http has the given port:

Example
http = Net::HTTP.new(hostname, 8000)
# => #<Net::HTTP jsonplaceholder.typicode.com:8000 open=false>
http.port # => 8000

Proxy Using Argument p_addr as a String

When argument p_addr is a string hostname, the returned http has a proxy:

Example
http = Net::HTTP.new(hostname, nil, 'proxy.example')
# => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false>
http.proxy?        # => true
http.proxy_address # => "proxy.example"
# These use default values.
http.proxy_port    # => 80
http.proxy_user    # => nil
http.proxy_pass    # => nil

The port, username, and password for the proxy may also be given:

Example
http = Net::HTTP.new(hostname, nil, 'proxy.example', 8000, 'pname', 'ppass')
# => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false>
http.proxy?        # => true
http.proxy_address # => "proxy.example"
http.proxy_port    # => 8000
http.proxy_user    # => "pname"
http.proxy_pass    # => "ppass"

Proxy Using ENV['http_proxy']

When environment variable 'http_proxy' is set to a URI string, the returned http will have that URI as its proxy; note that the URI string must have a protocol such as 'http' or 'https':

Example
ENV['http_proxy'] = 'http://example.com'
# => "http://example.com"
http = Net::HTTP.new(hostname)
# => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false>
http.proxy?        # => true
http.address       # => "jsonplaceholder.typicode.com"
http.proxy_address # => "example.com"

The URI string may include proxy username, password, and port number:

Example
ENV['http_proxy'] = 'http://pname:[email protected]:8000'
# => "http://pname:[email protected]:8000"
http = Net::HTTP.new(hostname)
# => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false>
http.proxy_port # => 8000
http.proxy_user # => "pname"
http.proxy_pass # => "ppass"

Argument p_no_proxy

You can use argument p_no_proxy to reject certain proxies:

  • Reject a certain address:

    Example
    http = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'proxy.example')
    http.proxy_address # => nil
    
  • Reject certain domains or subdomains:

    Example
    http = Net::HTTP.new('example.com', nil, 'my.proxy.example', 8000, 'pname', 'ppass', 'proxy.example')
    http.proxy_address # => nil
    
  • Reject certain addresses and port combinations:

    Example
    http = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'proxy.example:1234')
    http.proxy_address # => "proxy.example"
    
    http = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'proxy.example:8000')
    http.proxy_address # => nil
    
  • Reject a list of the types above delimited using a comma:

    Example
    http = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'my.proxy,proxy.example:8000')
    http.proxy_address # => nil
    
    http = Net::HTTP.new('example.com', nil, 'my.proxy', 8000, 'pname', 'ppass', 'my.proxy,proxy.example:8000')
    http.proxy_address # => nil