I have a simple Ruby script (Ruby 2.0) that I want to run on Mac OSX Yosemite and I want to proxy the network requests via Charles Proxy.
require 'json'
require 'net/http'
require 'net/https'
class Charles
def go
uri = URI.parse('http://example.com')
req = Net::HTTP::Get.new(uri.request_uri)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = false
res = http.request req
puts res
puts res.body
end
end
net = Charles.new
net.go
If I execute this script, all is fine but I don't see the request in Charles.
As soon as I set an http proxy via the command line, the script errors:
export http_proxy=192.168.1.108:8888
/Users/hug/.rvm/rubies/ruby-2.0.0-p643/lib/ruby/2.0.0/uri/common.rb:176:in `split': bad URI(is not URI?): 192.168.1.108:8888 (URI::InvalidURIError)
After I
unset http_proxy
it works again. I also tried
export http_proxy=locahost:8888
export http_proxy=machinename.local:8888
All these ways of setting the proxy work fine for curl, but none for Ruby.
What am I doing wrong?
The http_proxy environment variable should include the protocol as well. Curl seems to be lazy in that sense.
Try setting the proxy via
export http_proxy="http://localhost:8888"