# Overload rails 3.2.x default HTTP Baisc 401 response # to JSON instead of plain text/html. # # Examples # # require 'json_401' # class ApiController < ActionController::Metal # ... # end # module ActionController::HttpAuthentication::Basic def authentication_request(controller, realm) controller.headers["WWW-Authenticate"] = %(Basic realm="#{realm.gsub(/"/, "")}") controller.response.headers["Content-Type"] = "application/json" controller.response_body = {error: "Access denied."}.to_json controller.status = 401 end end