How to set a proxy in rubys net/http?

There is another option:

Net::HTTP will automatically create a proxy from the http_proxy environment variable if it is present.

So you can use

ENV['http_proxy'] = '' # your http://address:port here

and Net::HTTP will use it for all requests by default.

It can be helpful for net_http requests in third-party libraries (for example it works for gem gibbon for MailChimp).

Pass nil for the proxy address to disable default http_proxy.'', nil, proxy_addr, proxy_port).start { |http|

This will create an http object for you to use in the block. Use that rather than generating new ones each time, here Net::HTTP.get('', '')

proxy_addr = 'proxy'
proxy_port = 8080'', nil, proxy_addr, proxy_port).start { |http|
  # always proxy via your.proxy.addr:8080
  http.get('', '')

Here is the code that works if you are making a REST api call behind a proxy:

require "uri"
require 'net/http'
proxy_host = '<proxy addr>'
proxy_port = '<proxy_port'
proxy_user = '<username>'
proxy_pass = '<password>'
uri   = URI.parse("<username>")
proxy = Net::HTTP::Proxy(proxy_host,
req =

result = proxy.start(,uri.port) do |http|
puts result.body