rails/rails — Issue #3390: HTTP_ACCEPT doesn't allows extra attributes for MIME TYPE application/json
Author: Antek-drzewiecki
Created: 2011-10-21 13:48:02+00:00
State: open

Some mobile phone providers add compression parameters in their Accept request Headers. This might cause the application to respond with HTML instead of JSON.

I recently came across the following format for application/json:
HEADER KEY: HTTP_ACCEPT
HEADER VALUE: application/json,*/*;q=0.001

The rails application will translate this Accept header to html/text
HEADER KEY: action_dispatch.request.formats
HEADER VAL: text/html

The value application/json for HTTP_ACCESS will give a json callback.
I fixed this by forcing the request format to application/json in the ApplicationController

class ApplicationController < ActionController::Base
before_filter :foo
def foo
request.format = 'json'.to_sym if ((request.env["HTTP_ACCEPT"]).include? "application/json")
end

But proper handling would be better if the application/json,*/*;q=0.001 syntax is correct.