Proxying requests


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


would proxy through to

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 =
    if request.path =~ %r{^/oauth}
      env["HTTP_HOST"] = ""
      env['SERVER_PORT'] = 80

      env['SCRIPT_NAME'] = nil
      env['HTTP_X_FORWARDED_PORT'] = nil
      env['HTTP_X_FORWARDED_PROTO'] = nil

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