使用 Rack 中间件为每个请求添加api_key



我使用Devise token_authentication服务和ActiveResource客户端。我希望自动设置:在每个请求中auth_token参数!

我试过这个,但这不起作用...

class AuthApp
    def initialize(app)
        @app = app
    end
    def call(env)
        status, headers, response = @app.call(env)
        request = Rack::Request.new(env)
        request.params[:auth_token] = 'jCxKPj8wJJdOnQJB8ERy'
        [status, headers, response]
    end
end

知道吗?

如果您有包含此拉取请求的机架的最新副本,则可以使用 Rack::Request#update_param

request = Rack::Request.new(env)
request.update_param :auth_token, 'jCxKPj8wJJdOnQJB8ERy'

这将保留在中间件(和 Rails)之间传递的env中。

最新更新