Rails 3 has a nice feature where it will parse the body of PUT and POST requests depending on the Content-type given. So for example, if I POST XML to Rails, it will all be decoded for me and put into the params hash.

Well, it’s a nice feature until you try to switch it off, it’s a nightmare to disable! I managed it in the end, here’s what I did…

I created a piece of ‘rack middleware’ that overwrites the Content-Type for given paths:

# lib/no_parse.rb
class NoParse
  def initialize(app, options={})
    @app = app
    @urls = options[:urls]
  end

  def call(env)
    env['CONTENT_TYPE'] = 'text/plain' if @urls.include? env['PATH_INFO']
    @app.call(env)
  end
end

Included it:

#config/application.rb
require_relative '../lib/no_parse'

Added it into the ‘stack’:

#config/initializers/no_parse.rb

# (Change the :urls to specify which paths use NoParse.)
Rails.configuration.middleware.insert_before('ActionDispatch::ParamsParser',
                                             'NoParse', :urls => ['/example-path'])

Make sure you restart rails, and you should be good to go. If it’s not working, make sure the rake middleware command is listing the NoParse class above ActionDispatch::ParamsParser.

Cheers, Dave.