Mobile Proxy for Ruby
Ruby's standard Net::HTTP takes the proxy host, port, user and password as positional args on .new. HTTParty exposes http_proxyaddr and Faraday a proxy option. This guide wires all three to a mobile proxy and rotates the IP.
Prerequisites
- →Ruby 3.0+ (
ruby -v).net/httpis in the standard library. - →A mobile proxy slot, port, username, password and API key from mobileproxies.org.
- →Optional gems:
gem install httparty faraday.
Step-by-Step Configuration
Read credentials from the environment
# config.rb — load from ENV, never hard-code secrets
MP = {
host: ENV.fetch("MP_HOST", "proxy.mobileproxies.org"),
port: ENV.fetch("MP_HTTP_PORT", "8000").to_i,
user: ENV.fetch("MP_USER"),
pass: ENV.fetch("MP_PASS"),
api_key: ENV.fetch("MP_API_KEY"),
slot: ENV.fetch("MP_SLOT"),
}.freezeNet::HTTP (standard library)
require "net/http"
require "uri"
require_relative "config"
uri = URI("https://example.com")
# Net::HTTP.new(address, port, proxy_host, proxy_port, proxy_user, proxy_pass)
http = Net::HTTP.new(
uri.host, uri.port,
MP[:host], MP[:port], MP[:user], MP[:pass]
)
http.use_ssl = (uri.scheme == "https")
http.read_timeout = 30
response = http.get(uri.request_uri)
puts "status: #{response.code}"HTTParty
require "httparty"
require_relative "config"
response = HTTParty.get(
"https://example.com",
http_proxyaddr: MP[:host],
http_proxyport: MP[:port],
http_proxyuser: MP[:user],
http_proxypass: MP[:pass],
timeout: 30
)
puts "status: #{response.code}"Faraday
require "faraday"
require "uri"
require_relative "config"
# Faraday takes a single proxy URL with embedded credentials
proxy_url = "http://#{URI.encode_www_form_component(MP[:user])}:" \
"#{URI.encode_www_form_component(MP[:pass])}@" \
"#{MP[:host]}:#{MP[:port]}"
conn = Faraday.new(proxy: proxy_url) do |f|
f.options.timeout = 30
end
response = conn.get("https://example.com")
puts "status: #{response.status}"Verify It Works
Fetch api.ipify.org through the proxy and print the egress IP. It should be a carrier-owned mobile IP, never your machine's real address.
require "net/http"
require_relative "config"
ip = Net::HTTP.new(
"api.ipify.org", 443,
MP[:host], MP[:port], MP[:user], MP[:pass]
).tap { |h| h.use_ssl = true }.get("/").body
puts "Egress IP: #{ip}" # e.g. 100.42.x.x on a mobile ASNRotate the IP
Request a fresh carrier IP with a POST to the switch endpoint. This call goes direct (no proxy args) and authenticates with your API key:
require "net/http"
require "uri"
require_relative "config"
def rotate
uri = URI("https://buy.mobileproxies.org/api/v1/proxies/#{MP[:slot]}/switch")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri)
request["Authorization"] = "Bearer #{MP[:api_key]}"
response = http.request(request)
puts "rotate -> #{response.code}"
sleep 4 # let the new IP bind
endTroubleshooting
407 / Net::HTTPProxyAuthenticationRequired
The proxy user/pass args were nil or wrong. Confirm all four positional args (proxy_host, proxy_port, proxy_user, proxy_pass) are passed to Net::HTTP.new in that order.
OpenSSL certificate verify failed
Set http.use_ssl = true for HTTPS targets and keep http.verify_mode = OpenSSL::SSL::VERIFY_PEER. Update your CA bundle rather than disabling verification.
IP doesn't change after rotate
A persistent Net::HTTP.start block reuses one socket. Open a new connection after switching, or call rotate outside any start block so the next request opens a fresh tunnel.
Related Guides
Run Ruby Through Mobile IPs
$5 trial. Pass the proxy args to Net::HTTP, point at a carrier IP, and rotate on the API.