HTTParty with Proxy

10,418

Solution 1

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!

Solution 2

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.

Solution 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.

Solution 4

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

Solution 5

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
Share:
10,418
RidingRails
Author by

RidingRails

Updated on June 16, 2022

Comments

  • RidingRails
    RidingRails almost 2 years

    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.