7

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.

RidingRails
  • 782
  • 3
  • 7
  • 21

7 Answers7

3

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!

jakeonrails
  • 1,885
  • 15
  • 37
  • Thanks Jakeonrails! I switched to rest-client and went under HTTParty to Net::HTTP to no avail. In the end, and plenty of hours later, decided to install squid on a box we have and the problem is solved. – RidingRails Oct 05 '12 at 22:52
  • If I get a chance I will give your module a shot, but in crunch mode right now. – RidingRails Oct 05 '12 at 22:55
  • 1
    this just does not work. Ive actually never been able to make httparty work with a proxy. Ended up using rest-client. – Mike P. Nov 14 '13 at 23:12
3

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.
Arturo Herrero
  • 12,772
  • 11
  • 42
  • 73
disco crazy
  • 31,313
  • 12
  • 80
  • 83
3

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.

djangofan
  • 28,471
  • 61
  • 196
  • 289
3

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
Arturo Herrero
  • 12,772
  • 11
  • 42
  • 73
ouranos
  • 1,224
  • 10
  • 16
1

As you configure HTTParty as an inclusion in your module, you have to call HTTParty method through your class, so:

def make_post
  self.class.post "https://test.com", :query => set_defaults
end
caedes
  • 71
  • 4
0

I included a link because it include lengthy setup steps. But point taken about the link changing and page becoming invalid. We used squid proxy server on a EC2 AMI and called it a day.

RidingRails
  • 782
  • 3
  • 7
  • 21
-1

I think you need to call the local httparty, like this:

def make_post
  self.post "https://test.com", :query => set_defaults 
end
Petr Bela
  • 8,493
  • 2
  • 33
  • 37