5

When hitting an API in a Rails app, it seems that certain headers fail to be parsed. For example, this works:

Accept: application/json

But this doesn't:

Accept: application/json; charset=utf-8

Is failing to understand this 2nd header a legitimate problem with the header? Even if it is, how can I make Rails understand it, or at least understand that it's a request for Json?

1
  • What version of Rails are you using? Commented Sep 22, 2013 at 1:56

1 Answer 1

5

The issue is that, in Rails 3.2.14, the Mime::Type.parse method does not support specifying a charset parameter in the Accept header; in fact it only supports the "q" parameter (see the Q_SEPARATOR_REGEXP constant).

In Rails 4, however, the method has been updated to support arbitrary parameters (see this commit), so upgrading to Rails 4 should fix this problem.

If you can't upgrade to Rails 4, I would suggest doing a temporary hack in some Rack middleware to strip out the charset (this assumes you don't actually intend to honour the acceptable charset):

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

  def call(env)
    env["HTTP_ACCEPT"].gsub!(/;\s*charset=\S+/, "")
    @app.call(env)
  end
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.