ruby on rails - authenticate_or_request_with_http_token returning html instead of json -


i've created rails-api application , proceeded secure using token authentication.

i've set before_filter calling method uses authenticate_or_request_with_http_token. working fine but, when authentication incorrect i'm getting html response.

how can define format response should be?

before_filter :restrict_access  private  def restrict_access   authenticate_or_request_with_http_token |token, options|     check_token token   end end  def check_token(token)   session.exists?(access_token: token) end 

by including actioncontroller::httpauthentication::token::controllermethods include several methods, amongst others request_http_token_authentication wrapper around token.authentication_request. #authentication_request-method culprit , sends plain text (not html question suggests) follows:

def authentication_request(controller, realm)   controller.headers["www-authenticate"] = %(token realm="#{realm.gsub(/"/, "")}")   controller.__send__ :render, :text => "http token: access denied.\n", :status => :unauthorized end 

the trick override request_http_token_authentication in applicationcontroller not call token.authentication_request set correct status , headers , render json instead. add applicationcontroller:

protected def request_http_token_authentication(realm = "application")   self.headers["www-authenticate"] = %(token realm="#{realm.gsub(/"/, "")}")   render :json => {:error => "http token: access denied."}, :status => :unauthorized end 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -