Satishakumar Awati
Satishakumar Awati

Reputation: 3790

How to add new key value pair to request.env object in Rails

In rails controller following two lines of code are being used.

security_data = request.env['HTTP_X_SECURITY_DATA']
security_hash = request.env['HTTP_X_SECURITY_HASH']

I want to understand how/when/where these keys HTTP_X_SECURITY_DATA and HTTP_X_SECURITY_HASH are set to request.env hash or object.

I have read through this blog, but I did not get how to add new key-value of our own.

Any help appreciated, thanks.

Upvotes: 0

Views: 1396

Answers (1)

archana
archana

Reputation: 1272

You can add or delete any key in env by creating a middleware app:

class AddHeaderMiddleware
  def initialize(app)
    @app = app
  end

  def call(env)
    env['YOUR_KEY_HERE'] = 'your_value'
    @status, @headers, @response = @app.call(env)
    [@status, @headers, @response]
  end
end

You can add key to env inside call method.

Upvotes: 2

Related Questions