简体   繁体   中英

Devise - Force JSON Response

How can I force the response to JSON on Devise?

When my controllers require authentication with

before_filter :authenticate_user!

the response is only in JSON if I post JSON data. But I want Devise to respond always in JSON for some controllers and actions.

EDIT

I am using

:token_authenticatable

so I am passing auth_token for every call:

curl --data "token=xyzHtPAFGuAuFiVWsxsZ&receipt=adsfadsfsdfdsf" http://0.0.0.0:3000/user/receipt

But the response is a HTML redirect when the token is incorrect

<html><body>You are being <a href="http://0.0.0.0:3000/users/sign_in">redirected</a>.</body></html>

I'd like that response to be a JSON

I'm not sure if I understood your question but this might be what you are looking for. Just add respond_to inside your controller like:

class MyController < ApplicationController
  respond_to :json

  ...
end

You need to define the content type of the desired response.

curl -X POST \
     -H "Content-Type:application/json" \
     -H "Accept:application/json \
     -d "token=xyzHtPAFGuAuFiVWsxsZ&receipt=adsfadsfsdfdsf" \
      http://0.0.0.0:3000/user/receipt

Please have a look at the man page of curl for further parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM