title: Recurrent invalid byte sequence in UTF-8 with Rails
body: <h2>Environment</h2>
  
  <ul>
  <li>Rails 3.2.11</li>
  <li>Ruby 1.9.1</li>
  <li><code>utf8-cleaner</code> gem</li>
  </ul>
  
  <p>I am struggling with this issue for over a year now, and I am not able to reproduce it on my dev environment, which makes it rather difficult for me to understand why this is happening and how I can resolve. Here is the error notification I am getting (via email using ExceptionNotifier):</p>
  
  <pre><code>A ArgumentError occurred in home#index:
  
    invalid byte sequence in UTF-8
    .bundle/gems/ruby/1.9.1/gems/rack-1.4.5/lib/rack/utils.rb:104:in `normalize_params'
  </code></pre>
  
  <p>Apparently caused by Chinese spider:</p>
  
  <pre><code> HTTP_USER_AGENT      : Mozilla/5.0 (compatible; EasouSpider; +http://www.easou.com/search/spider.html)
  </code></pre>
  
  <p>I have tried to a couple things, see my <a href="https://stackoverflow.com/questions/24722176/how-do-i-catch-argumenterror-for-my-whole-app-in-rails">earlier question</a> where I was attempting to catch the error. </p>
  
  <p>I have also installer <code>utf8-cleaner</code> gem but that doesn't seem to resolve, unless I missed a step.</p>
  
  <p>How can I reproduce the issue? Note the URL causing the problem is perfectly correct when I access it (?)</p>
  
  <h2>UPDATE 20140721 - use <a href="https://github.com/whitequark/rack-utf8_sanitizer" rel="nofollow noreferrer">rack-utf8_sanitizer</a></h2>
  
  <ul>
  <li>Add <code>gem 'rack-utf8_sanitizer'</code> in <code>Gemfile</code></li>
  <li>Add <code>config.middleware.insert 0, Rack::UTF8Sanitizer</code> in <code>application.rb</code></li>
  <li><code>$ bundle install</code></li>
  </ul>
  
  <p>That worked perfectly on DEV but FAILED on my Heroku PRODUCTION,  with the following issue:</p>
  
  <pre><code>$ heroku run rake middleware --a test-app
  Running `rake middleware` attached to terminal... up, run.4846
  WARNING: Nokogiri was built against LibXML version 2.8.0, but has dynamically loaded 2.7.6
  rake aborted!
  uninitialized constant Rack::UTF8Sanitizer
  /app/config/application.rb:71:in `&lt;class:Application&gt;'
  </code></pre>
  
  <p>I am still investigating why I should be getting this.</p>