Ruby Proxy Configuration Examples

This article describes options for configuring the Proxy Host with Basic Authentication in the Ruby scripting language for an HTTP client. For Ruby HTTP clients, if you are using IP authentication, you can leave out the username:password parts.

Proxy Setup with Net::HTTP

To set up a proxy for web requests in Ruby, use Ruby-specific libraries and syntax. The following code example sets up a proxy via the widely used Net::HTTP library in Ruby, which can make HTTP requests. After requiring the Net::HTTTP library, it defines the proxy settings, then creates an instance of Net::HTTP that includes those settings. Ruby’s closing puts command displays the result.

# Define the proxy settings
proxy_uri = URI.parse('http://proxy.example.com:8080')
proxy_user = 'username'
proxy_pass = 'password'

# Create a Net::HTTP object with proxy settings
http = Net::HTTP.new('example.com', nil, proxy_uri.host, proxy_uri.port, proxy_user, proxy_pass)

# Use 'http' to make requests through the proxy
response = http.get('/path/to/resource')

# Process response as needed
puts response.body

Proxy Setup with Typhoeus

You can also set up a proxy using Typhoeus, a popular HTTP client library for Ruby.

First install the Typhoeus gem.

gem install typhoeus

Then create an HTTP request with a proxy using Typhoeus. The proxy is defined within the request, specifying the URL and port.

require 'typhoeus'

# Define the URL you want to request
url = 'https://example.com'

# Define proxy settings
proxy_options = {
  proxy: 'http://proxy.example.com:8080', # Proxy server address and port
  proxytype: Typhoeus::PROXY_HTTP, # Proxy type (HTTP in this case)
  proxyuserpwd: 'username:password' # Optional: Proxy username and password
}

# Create a Typhoeus request object
request = Typhoeus::Request.new(url, proxy: proxy_options)

# Perform the request
response = request.run

# Check if the request was successful
if response.success?
  puts "Response body: #{response.body}"
else
  puts "Request failed with status code: #{response.code}"
  puts "Error message: #{response.return_message}"
end

The Typhoeus library offers several useful features, including the Hydra class, which can help you distribute requests in parallel.

Hydra for multiple requests

hydra = Typhoeus::Hydra.new
requests = (0..9).map{ Typhoeus::Request.new("www.example.com") }
requests.each{ |request| hydra.queue(request) }
hydra.run

HTTParty Code Example

Below is an example showing how to setup the HTTParty Ruby library for using a proxy with username/password authentication. HTTParty helps you make HTTP requests from Ruby and communicate with web services.  It returns a response object including headers and data content.

class Foo
  include HTTParty
  http_proxy 'http://proxy.example.com', 3128, 'username', 'pass'

If you’re making HTTPS requests, see Proxy Server Requests over HTTPS for more details. IP authentication is usually the best option, and for that you can omit the username & pass above.  If you change your own IP address regularly, one way to make it work is with dynamic DNS and domain authentication. You can find more details in ProxyMesh’s help doc Proxy Authentication.

Still need help? Contact Us Contact Us