HTTParty with Proxy

RidingRails picture RidingRails · Oct 5, 2012 · Viewed 9.6k times · Source

I am on heroku trying to access an API that requires my apps ip to be whitelisted. So, I used the heroku add-on proximo to get host/ip for the api's whitelist.

A quick test I set up to test connectivity using HTTParty is failing.

class FakeRequest
 include HTTParty
 http_proxy 'XX.XXX.XX.XX', 80, 'user', 'pass'


 def set_defaults
   {:api_key=>"BLARG_BLARG",
    :login_name=>"user",
    :method => "do_something",
    :response_format => "json", 
    :v => "1.0",
    :login_password=>"pass"}
end


 def make_post
  HTTParty.post "https://test.com", :query => set_defaults 
 end
end 

Going this like: req = FakeRequest.new req.make_post

Returns an error message from the api complaining that the source IP is not whitelisted. I looked at the source IP and it is not using the proxy. How can I make HTTParty post using the proxy and not my ISP's IP.

Answer

jakeonrails picture jakeonrails · Oct 6, 2012

This is the module I built to do just this:

module ProximoParty

  PROXIMO = URI.parse(ENV['PROXIMO_URL'])

  def self.included(base)
    base.send(:include, HTTParty)
    base.http_proxy(PROXIMO.host, 80, PROXIMO.user, PROXIMO.password)
  end

end

This uses the PROXIMO URL as it is added to your heroku app when you install the addon. So you can drop this file into your app and include ProximoParty into your FakeRequest class instead of HTTParty and it should "just work".

It looks like my code is doing the same thing your code is doing though, so what I'm guessing is that you may not be manually carrying over the credentials properly for proximo.

I ran into a similar problem where it wasn't quite working for me right off the bat. I believe the problem was that I was getting tripped up that there looked to be a "proxy:" protocol in the proximo URL but that was just the username part of the URL.

Anyways, this may or may not help, but please let me know if it does!