title: Rack custom middleware giving error "ActionDispatch::Http::Parameters::ParseError"
body: <p>I am writing a custom middleware for my rails app, to decrypt/encrypt parameters in a post request. I have configured the middleware in <code>'config/environments/staging.rb'</code> </p>
  
  <pre><code>config.middleware.use CustomMiddleware
  </code></pre>
  
  <p>In order to read the request params i am reading the env obj like this </p>
  
  <pre><code>rack_input = env['rack.input'].read
  </code></pre>
  
  <p>I am able to access the parameter and manipulate as needed. But when i pass over on the <code>env</code> obj to the app object which is the receiver. I am getting </p>
  
  <pre><code>"ActionDispatch::Http::Parameters::ParseError" 
  </code></pre>
  
  <p>Full error</p>
  
  <pre><code>Error occurred while parsing request parameters.
  Contents:
  
  
  F, [2017-11-15T02:13:50.230088 #24060] FATAL -- : [9aa25c7e-56e7-4894-ba30-3a01f60ae4fc]   
  F, [2017-11-15T02:13:50.230340 #24060] FATAL -- : [9aa25c7e-56e7-4894-ba30-3a01f60ae4fc] ActionDispatch::Http::Parameters::ParseError (no implicit conversion of nil into String):
  F, [2017-11-15T02:13:50.230563 #24060] FATAL -- : [9aa25c7e-56e7-4894-ba30-3a01f60ae4fc]   
  F, [2017-11-15T02:13:50.230782 #24060] FATAL -- : [9aa25c7e-56e7-4894-ba30-3a01f60ae4fc] app/middleware/custom_middleware.rb:34:in `call'
  </code></pre>
  
  <p>My code is somewhat like this </p>
  
  <pre><code>class CustomMiddleware
    include Encryption   
  
    def initialize(app)
      @app = app
    end
  
    def set_payload_params(env)
      rack_input = env['rack.input'].read
      @args = JSON.parse(rack_input) rescue {}
    end
  
    def call(env)
      request = Rack::Request.new(env)
      set_payload_params(env)
  
      payload = @args['payload']
  
      decrypt_params
      status, headers, response = @app.call(env)
      encrypt_params
  
    end
  </code></pre>
  
  <p>If i don't read the env object and hard code the parameters, there is no such parsing error. And my controller is responding with data w.r.t to hardcode parameters. </p>
  
  <p>My Rails version is 5.1.4
  My ruby version is 2.4.2</p>
  
  <p>I have done a similar middleware in another rails app(4.2.8) which is working fine. </p>
  
  <p>Any insight on this will be of much help. Thanks in advance.</p>