Proxying requests

Hi,

I’m trying to proxy some requests from a javascript front-end through to an external API using Rails to fix any CORS issues but have not yet been able to get it working properly.

Ideally, a url request from the javascript app in this format

api/oauth/connect.php?secret=test

would proxy through to

https://3rdparty.com/oauth/connect?secret=test

I’ve been using rack-proxy to try to achieve this.

I can get it working if I remove the api/ namespace and include in my application.rb the line

config.middleware.use "MyProxy"

Along with a proxy like this

require 'rack-proxy'

class MyProxy < Rack::Proxy
  def perform_request(env)
    request = Rack::Request.new(env)
    if request.path =~ %r{^/oauth}
      env["HTTP_HOST"] = "3rdparty.com"
      env['SERVER_PORT'] = 80

      env['SCRIPT_NAME'] = nil
      env['HTTP_X_FORWARDED_PORT'] = nil
      env['HTTP_X_FORWARDED_PROTO'] = nil
      super(env)
    else
      @app.call(env)
    end
  end
end

But no matter what I’ve tried I haven’t been able to map the URLs in the way I was hoping, including trying to mount the rack app in the routes like this mount MyProxy.new, at: '/api'

I’ve also noted that this approach now logs two requests.

Any help would be very much appreciated, I’m finding it very difficult to debug, as even using Charles proxy application I can’t discover what the actual request URL Rails makes to the 3rd party server is.

Thanks!