Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTParty with Proxy

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.

like image 364
RidingRails Avatar asked Oct 05 '12 16:10

RidingRails


4 Answers

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!

like image 170
jakeonrails Avatar answered Oct 23 '22 05:10

jakeonrails


It blows my mind how many answers you can get on this question that don't work. Here is how I got it working:

HTTParty::Basement.http_proxy('localhost', 8000, nil, nil)

Put this as a global override in your env.rb file. That's it. Done.

like image 38
djangofan Avatar answered Oct 23 '22 05:10

djangofan


With recent versions of HTTParty it's as simple as using the http_proxy method:

class Foo
  include HTTParty
  http_proxy 'http://example.com', 80, 'user', 'pass'
end
like image 3
ouranos Avatar answered Oct 23 '22 06:10

ouranos


HTTParty can use a proxy server address using the following proxy options.

[:+http_proxyaddr+:] Address of proxy server to use
[:+http_proxyport+:] Port of proxy server to use.
[:+http_proxyuser+:] User for proxy server authentication
[:+http_proxypass+:] Password for proxy server authentication.
like image 3
H6. Avatar answered Oct 23 '22 06:10

H6.