# Forcing all http requests to follow system proxy configuration

**URL:** <https://www.ruby-forum.com/t/forcing-all-http-requests-to-follow-system-proxy-configuration/209631>\
**Category:** Ruby\
**Created:** [July 19, 2011, 7:47am UTC](https://www.ruby-forum.com/t/forcing-all-http-requests-to-follow-system-proxy-configuration/209631 "2011-07-19T07:47:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [July 19, 2011, 7:47am UTC](https://www.ruby-forum.com/t/forcing-all-http-requests-to-follow-system-proxy-configuration/209631/1 "2011-07-19T07:47:02Z")

</div>

I am working with a ruby application that uses some loosely coupled  
modules that interface with a web service. These modules use net::http  
module of ruby for network communication. But the original code has no  
means to specify proxy configurations. And of course ruby’s net::http  
does not respect system proxy configuration.

I would like to know if there is a way to channelize all the http  
requests through the proxy server specified by path variable  
‘http\_proxy’ without changing these modules. (These modules are  
separately maintained and making hard changes would make things  
difficult while updating them in future)

Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![eunikorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@eunikorn](https://www.ruby-forum.com/u/eunikorn)\
**Post date:** [July 19, 2011, 8:29am UTC](https://www.ruby-forum.com/t/forcing-all-http-requests-to-follow-system-proxy-configuration/209631/2 "2011-07-19T08:29:15Z")

</div>

[…]

> And of course ruby’s net::http  
> does not respect system proxy configuration.

You can make it respect the system proxy configuration:

require ‘net/http’  
proxy\_uri = URI.parse(ENV[‘http\_proxy’])  
proxy\_class = Net::HTTP::Proxy(proxy.host, proxy.port)

proxy\_class.start(‘[www.example.com](http://www.example.com)’) do |http|  
# whatever  
end

> I would like to know if there is a way to channelize all the http  
> requests through the proxy server specified by path variable  
> ‘http\_proxy’ without changing these modules. (These modules are  
> separately maintained and making hard changes would make things  
> difficult while updating them in future)

So you can probably channelize all your http requests through the  
proxy\_class.

---

<div class="post-metadata">

**Author:** ![eunikorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@eunikorn](https://www.ruby-forum.com/u/eunikorn)\
**Post date:** [July 19, 2011, 8:31am UTC](https://www.ruby-forum.com/t/forcing-all-http-requests-to-follow-system-proxy-configuration/209631/3 "2011-07-19T08:31:07Z")

</div>

> require ‘net/http’  
> proxy\_uri = URI.parse(ENV[‘http\_proxy’])  
> proxy\_class = Net::HTTP::Proxy(proxy.host, proxy.port)

I meant,

proxy\_class = Net::HTTP::Proxy(proxy\_url.host, proxy\_url.port)

---

<div class="post-metadata">

**Author:** ![eunikorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@eunikorn](https://www.ruby-forum.com/u/eunikorn)\
**Post date:** [July 19, 2011, 11:35pm UTC](https://www.ruby-forum.com/t/forcing-all-http-requests-to-follow-system-proxy-configuration/209631/4 "2011-07-19T23:35:40Z")

</div>

I’ve been working on a gem that lets you force TCPSocket to use a proxy.  
I haven’t pushed it to rubygems yet but you can clone my repo and  
install it  
and try it out.

> **[GitHub - samuelkadolph/ruby-proxifier](https://github.com/samuelkadolph/ruby-proxifier)**
>
> Contribute to samuelkadolph/ruby-proxifier development by creating an account on GitHub.

On Tue, Jul 19, 2011 at 2:30 AM, Anurag P.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> [http://about.me/yeban/](http://about.me/yeban/)

–

\*Samuel K.  
\*E [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![eunikorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@eunikorn](https://www.ruby-forum.com/u/eunikorn)\
**Post date:** [July 20, 2011, 2:18pm UTC](https://www.ruby-forum.com/t/forcing-all-http-requests-to-follow-system-proxy-configuration/209631/5 "2011-07-20T14:18:20Z")

</div>

@Anurag  
Thank you for your answer. I really didnt expect you to post a reply.  
What you are suggesting is definitely the standard way to use a proxy in  
net::http with the proxy configuration being retrieved from the  
environment variables

But as you see, in your code :

proxy\_class.start(‘[www.example.com](http://www.example.com)’) do |http|  
# whatever  
end

I do not always have the url to which the request is being POSTed. In  
fact the request is already being made by the third party code using  
something like :

```
res = Net::HTTP.start(url.host, url.port) {|http|
  # whatever
}

```

So what I really meant was that if I could force this request to follow  
the proxy configurations without changing the code.

---

<div class="post-metadata">

**Author:** ![eunikorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@eunikorn](https://www.ruby-forum.com/u/eunikorn)\
**Post date:** [July 20, 2011, 4:19pm UTC](https://www.ruby-forum.com/t/forcing-all-http-requests-to-follow-system-proxy-configuration/209631/6 "2011-07-20T16:19:38Z")

</div>

rake install works for me in 1.9.2 and 1.8.7. It does use bundler so you  
have to have that gem installed.  
If that’s not the case would you mind posting the output to  
[http://pastie.org/](http://pastie.org/) so I can look at it?  
Thanks.

On Wed, Jul 20, 2011 at 8:20 AM, eunikorn imazinator  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> @samuelkadolph  
> Thank you for your fantastic contribution.  
> Although the rake install failed for me (could it be a version issue ? )  
> I could build the gem and install it .  
> Works like a charm. Thank you again.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

–

\*Samuel K.  
\*E [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![eunikorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@eunikorn](https://www.ruby-forum.com/u/eunikorn)\
**Post date:** [July 20, 2011, 2:20pm UTC](https://www.ruby-forum.com/t/forcing-all-http-requests-to-follow-system-proxy-configuration/209631/7 "2011-07-20T14:20:47Z")

</div>

@samuelkadolph  
Thank you for your fantastic contribution.  
Although the rake install failed for me (could it be a version issue ? )  
I could build the gem and install it .  
Works like a charm. Thank you again.
