0

My app works great on localhost. Our Cloud Foundry setup somehow does not have outbound connectivity... so I have been told I can get outbound connectivity through a proxy.

What do I need to change in my Faraday requests to use that proxy?

My code and how I am supposed to connect.

url = "#{@config[:host]}#{@config[:login_path]}"
c = Faraday.new(:url => url,
                :request => { :timeout => 20, :open_timeout => 5},
                :ssl => {:verify => false}) do |f|
  f.request :url_encoded
  f.adapter Faraday.default_adapter
  f.response :json, :content_type => /\bjson$/
end

  @resp = c.post(url, {username: username, password: password,
    deviceId: @config[:device_id], deviceName: @config[:device_name], deviceType: @config[:device_type],
    appId: @config[:app_id], scope: @config[:scope]})

This is how I am supposed to use the proxy that was set up.

"Once your service is bound, the application will have service definition in JSON format in VCAP_SERVICES environment variable you can see the format using cf env x-explorer command. Your app will need to parse and setup proxy using the given credentials.

Here is an example of the credentials. You can use only uri or individual parameters: host, port, username and password"

uri: http://2abcf906-127e-495c-ae71-c69712862292:616e714aea0cfc81de67ad1445cf1448e38d6a8db345a452c0e1ddc830b8f132@f.services.g1.app.cloud.my_company.net:49349
host: f.services.g1.app.cloud.my_company.net
port: 49349
username: 2abcf906-127e-495c-ae71-c69712862292
password: 616e714aea0cfc81de67ad1445cf1448e38d6a8db345a452c0e1ddc830b8f132

Again, app works great on localhost. No outbound connectivity with company Cloud Foundry setup. I have bound the service, so that is fine.

What do I add to have my outbound requests go through the proxy?

slindsey3000
  • 4,053
  • 5
  • 36
  • 56

1 Answers1

2

Try this

url = "#{@config[:host]}#{@config[:login_path]}"
c = Faraday.new(:url => url,
                :request => { :timeout => 20, :open_timeout => 5},
                :ssl => {:verify => false}) do |f|
  f.request :url_encoded
  f.adapter Faraday.default_adapter
  f.response :json, :content_type => /\bjson$/url = "#{@config[:host]}#{@config[:login_path]}"
c = Faraday.new(:url => url,
                :request => { :timeout => 20, :open_timeout => 5},
                :ssl => {:verify => false}) do |f|
  f.request :url_encoded
  f.adapter Faraday.default_adapter
  f.response :json, :content_type => /\bjson$/
  f.proxy = "http://2abcf906-127e-495c-ae71-c69712862292:616e714aea0cfc81de67ad1445cf1448e38d6a8db345a452c0e1ddc830b8f132@f.services.g1.app.cloud.my_company.net:49349"
end

  @resp = c.post(url, {username: username, password: password,
    deviceId: @config[:device_id], deviceName: @config[:device_name], deviceType: @config[:device_type],
    appId: @config[:app_id], scope: @config[:scope]})

end

  @resp = c.post(url, {username: username, password: password,
    deviceId: @config[:device_id], deviceName: @config[:device_name], deviceType: @config[:device_type],
    appId: @config[:app_id], scope: @config[:scope]})

Here they says how to set up a proxy on faraday request doing

conn = Faraday.new(url, ssl: {verify:false}) do |conn|
  # middleware ...
  conn.adapter <adapter>

  conn.proxy "http://localhost:80"
end

https://github.com/lostisland/faraday/issues/221

Horacio
  • 2,865
  • 1
  • 14
  • 24
  • Thank you. I will try it on Monday morning sir! – slindsey3000 Jul 25 '15 at 13:30
  • What about the username and password I am supposed to use for the proxy? It looks like I have to log into the proxy with Faraday... See the second code block in my question. – slindsey3000 Jul 26 '15 at 03:58
  • 1
    look at http://www.rubydoc.info/github/lostisland/faraday/Faraday/Connection :proxy - URI, String or Hash of HTTP proxy options (default: "http_proxy" environment variable). :uri - URI or String :user - String (optional) :password - String (optional) – Horacio Jul 26 '15 at 04:33