Mechanize Proxy Configuration

← Back to Ruby Libraries

Mechanize automates websites like a browser. Call set_proxy with host, port, and optional credentials parsed from your proxy URL, as in proxy-examples/ruby/mechanize-proxy.rb. Mechanize does not integrate with ruby-proxy-headers directly; for sticky IP or country headers over HTTPS, fetch with Faraday + ruby-proxy-headers or patched Net::HTTP, then parse pages with Mechanize if needed.

Installation

gem install mechanize

Basic Proxy Configuration

require 'mechanize'
require 'uri'

proxy = URI('http://username:password@proxyhost:31280')

agent = Mechanize.new
agent.set_proxy(proxy.host, proxy.port, proxy.user, proxy.password)

page = agent.get('https://api.ipify.org?format=json')
puts page.body

Custom Proxy Headers

Use Net::HTTP + RubyProxyHeaders::NetHTTP.patch! or Faraday + RubyProxyHeaders::FaradayIntegration when you need X-ProxyMesh-Country or similar on CONNECT. See ruby-proxy-headers.


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