Home Rack custom middleware giving error "ActionDispatch::Http::Parameters::ParseError"
Reply: 1

Rack custom middleware giving error "ActionDispatch::Http::Parameters::ParseError"

RubyMiner
1#
RubyMiner Published in 2017-11-14 21:07:43Z

I am writing a custom middleware for my rails app, to decrypt/encrypt parameters in a post request. I have configured the middleware in 'config/environments/staging.rb'

config.middleware.use CustomMiddleware

In order to read the request params i am reading the env obj like this

rack_input = env['rack.input'].read

I am able to access the parameter and manipulate as needed. But when i pass over on the env obj to the app object which is the receiver. I am getting

"ActionDispatch::Http::Parameters::ParseError" 

Full error

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'

My code is somewhat like this

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

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.

My Rails version is 5.1.4 My ruby version is 2.4.2

I have done a similar middleware in another rails app(4.2.8) which is working fine.

Any insight on this will be of much help. Thanks in advance.

RubyMiner
2#
RubyMiner Reply to 2017-11-15 09:45:47Z

Added a hack to set content-type as "" in custom middleware, which avoided parsing on the input before forwarding the input to next component in the stack.

 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)
     env['CONTENT_TYPE'] = ''
     request = Rack::Request.new(env)
     set_payload_params(env)

     payload = @args['payload']

     decrypt_params
     status, headers, response = @app.call(env)
     encrypt_params

  end
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.346709 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO