Created
April 20, 2012 19:40
-
-
Save zerobearing2/2431247 to your computer and use it in GitHub Desktop.
Rails 3.2.x HTTP Basic 401 as JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment