Net::HTTP Proxy Configuration

← Back to Ruby Libraries

Net::HTTP is Ruby’s standard library HTTP client. It supports HTTP and HTTPS through an HTTP proxy using a CONNECT tunnel. For custom headers on that tunnel (for example X-ProxyMesh-Country), use the ruby-proxy-headers gem’s Net::HTTP patch.

Installation

Net::HTTP is part of Ruby—install only ruby-proxy-headers for advanced CONNECT headers:

gem install ruby-proxy-headers

Basic Proxy Configuration

Pass proxy host, port, user, and password to Net::HTTP.new (same pattern as proxy-examples/ruby/net-http-proxy.rb):

require 'net/http'
require 'openssl'
require 'uri'

uri = URI('https://api.ipify.org?format=json')
proxy = URI('http://username:password@proxyhost:31280')

http = Net::HTTP.new(
  uri.host, uri.port,
  proxy.host, proxy.port,
  proxy.user, proxy.password
)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER

response = http.request(Net::HTTP::Get.new(uri))
puts response.body

IP authentication

If your server IP is whitelisted, use a URL without credentials:

proxy = URI('http://proxyhost:port')

Custom Proxy Headers

require 'uri'
require 'openssl'
require 'ruby_proxy_headers/net_http'

RubyProxyHeaders::NetHTTP.patch!

uri = URI('https://api.ipify.org?format=json')
proxy = URI('http://username:password@proxyhost:31280')

http = Net::HTTP.new(
  uri.host, uri.port,
  proxy.host, proxy.port,
  proxy.user, proxy.password
)
http.use_ssl = true
http.proxy_connect_request_headers = { 'X-ProxyMesh-Country' => 'US' }

res = http.request(Net::HTTP::Get.new(uri))
puts res.body
puts http.last_proxy_connect_response_headers['X-ProxyMesh-IP']

Country headers require a proxy host that supports them—see world proxy or open proxy.


ProxyMesh Headers Reference

Send these headers to control proxy behavior:

  • X-ProxyMesh-Country - Route through a specific country (e.g., "US"). Only works with world proxy or open proxy
  • X-ProxyMesh-IP - Request a specific outgoing IP address
  • X-ProxyMesh-Not-IP - Exclude specific IPs from rotation

The proxy returns X-ProxyMesh-IP with the IP address used for the request (when your stack exposes CONNECT response headers).

Resources

Related Ruby Proxy Guides

Explore proxy configuration for other Ruby HTTP libraries:

Start Free Trial