How do I configure a Ruby Mechanize agent to work through the Charles web proxy?

charles-proxy, mechanize, proxy, ruby

Solution

A web proxy is not normally defined by just a port, but is usually a full host name. Charles is very likely installed on localhost. Therefore the following adjustment may work for you:

@agent ||= Mechanize.new do |agent|
  agent.set_proxy("localhost", 8888)
end

Problem

I'm writing an "automatically fill in the forms" app using Ruby / Mechanize. It almost works. I can use the wonderful Charles web proxy to see the exchange between the server and my Firefox browser. Now I want to use Charles to see the exchange between the server and my app. Charles proxies on port 8888. Assume that the server is at https://my.host.com. One thing that does NOT work is: ``` @agent ||= Mechanize.new do |agent| agent.set_proxy("my.host.com", 8888) end ``` This results in a `Net::HTTP::Persistent::Error`: ``` ...lib/net/http/persistent.rb:579:in `rescue in connection_for': connection refused: my.host.com:8888 (Net::HTTP::Persistent::Error) ``` So either I'm giving the wrong host argument to `agent.set_proxy(host, ...)`, or I haven't configured Charles properly. (FWIW, I used to be able to do this, but both Mechanize and Charles have matured several generations since those halcyon days...) Any ideas?

Original source