Api :: V1 :: UsersController#createのActionController InvalidAuthenticityToken

AndroidDev

Ruby onRailsを使用してRESTfulAPIを作成します。データを作成、削除、表示、更新したい。Androidデバイスで取得するには、すべてJSONである必要があります。また、Postmanを使用してAPIをチェックしています。これは私がしたことです:

私のコントローラー:

class Api::V1::UsersController < ApplicationController
    respond_to :json

    def show
        respond_with User.find(params[:id])
    end

    def create
        user=User.new(user_params)
        if user.save
            render json: user, status: 201
        else
            render json: {errors: user.errors}, status: 422
        end
    end

    def update
        user=User.find(params[:id])
        if user.update(user_params)
            render json: user, status:200
        else
        render json: {erros: user.errors},status: 422
        end

    end

    def destroy
        user=User.find(params[:id])
        user.destroy
        head 204
    end

    private
    def user_params
        params.require(:user).permit(:email,:password,:password_confirmation)
    end
end

これは私のルートファイルです:

Rails.application.routes.draw do
  devise_for :users
  namespace :api, defaults:{ format: :json } do
    namespace :v1 do
    resources :users, :only=>[:show,:create,:update,:destroy]   
    end
  end
end

また、Gemfileに次のコードを追加しました。

gem "devise"
gem 'active_model_serializers'

郵便配達員を介して作成したいときに、次のエラーが発生する理由がわかりません。

ActionController InvalidAuthenticityToken in Api::V1::UsersController#create
ディーパックマハカレ

application_controller.rbに次の変更を加える必要があります

変化する

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
end

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :null_session
end

編集

より良い方法は、特定のコントローラーの認証をスキップすることです

class Api::V1::UsersController < ApplicationController
  skip_before_action :verify_authenticity_token

  respond_to :json
  # ...
end

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

ActionController :: InvalidAuthenticityToken

分類Dev

ActionController :: RoutingError:初期化されていない定数Api :: V1 :: ApiController

分類Dev

ActionController :: RoutingError:初期化されていない定数Api :: V1 :: ApiController

分類Dev

ActiveAdmin :: Devise :: SessionsController#createのActionController :: InvalidAuthenticityToken

分類Dev

Rails上のrubyのActionController :: InvalidAuthenticityToken

分類Dev

Intermittent Rails 5 ActionController::InvalidAuthenticityToken

分類Dev

通常のActionController :: InvalidAuthenticityToken例外-Rails

分類Dev

Rails5のActionController :: InvalidAuthenticityToken

分類Dev

SessionsController#destroyのActionController :: InvalidAuthenticityToken

分類Dev

HTTP v1 API:Webプッシュ通知用の「click_action」

分類Dev

/ api / v1以外のapiリクエストは403 "Forbidden"を返します

分類Dev

Rails 5 ActionController :: InvalidAuthenticityTokenエラー

分類Dev

Rails 5 ActionController :: InvalidAuthenticityToken on Production Enviroment Nginx

分類Dev

Rails4でAPIを作成する-初期化されていない定数Api :: V1 :: UsersController

分類Dev

Rails6の別のActionController :: InvalidAuthenticityToken

分類Dev

tensorflow / hardutilsモジュールの_api / v1 / hard /とpython / hardの間の混乱

分類Dev

Golangのgithub.com/google/google-api-go-client/customsearch/v1の使用方法

分類Dev

API V1 から API V2 への Objective-C ドロップボックス SDK の移行

分類Dev

FCM HTTP V1 API returns 404 for unregistered token

分類Dev

FCM HTTP V1 API returns 404 for unregistered token

分類Dev

How to sort response by coinmarketcap api v1 by price in usd?

分類Dev

How to remove a PayPal product from v1/catalogs API?

分類Dev

Payment issue with PayPal REST API v1

分類Dev

Rails6-HerokuのInvalidAuthenticityToken

分類Dev

/ api / v1 / **へのすべてのリクエストを古いAPIサーバーに渡す

分類Dev

ActionController :: InvalidAuthenticityToken Rails 5 / Devise / Audited / PaperTrail gem

分類Dev

断続的なRails5 ActionController :: InvalidAuthenticityToken

分類Dev

Rails4 ActionController :: InvalidAuthenticityTokenエラー

分類Dev

Expressで/ api / v1 /(ここではどのルートでも)を使用する意味は何ですか?

Related 関連記事

  1. 1

    ActionController :: InvalidAuthenticityToken

  2. 2

    ActionController :: RoutingError:初期化されていない定数Api :: V1 :: ApiController

  3. 3

    ActionController :: RoutingError:初期化されていない定数Api :: V1 :: ApiController

  4. 4

    ActiveAdmin :: Devise :: SessionsController#createのActionController :: InvalidAuthenticityToken

  5. 5

    Rails上のrubyのActionController :: InvalidAuthenticityToken

  6. 6

    Intermittent Rails 5 ActionController::InvalidAuthenticityToken

  7. 7

    通常のActionController :: InvalidAuthenticityToken例外-Rails

  8. 8

    Rails5のActionController :: InvalidAuthenticityToken

  9. 9

    SessionsController#destroyのActionController :: InvalidAuthenticityToken

  10. 10

    HTTP v1 API:Webプッシュ通知用の「click_action」

  11. 11

    / api / v1以外のapiリクエストは403 "Forbidden"を返します

  12. 12

    Rails 5 ActionController :: InvalidAuthenticityTokenエラー

  13. 13

    Rails 5 ActionController :: InvalidAuthenticityToken on Production Enviroment Nginx

  14. 14

    Rails4でAPIを作成する-初期化されていない定数Api :: V1 :: UsersController

  15. 15

    Rails6の別のActionController :: InvalidAuthenticityToken

  16. 16

    tensorflow / hardutilsモジュールの_api / v1 / hard /とpython / hardの間の混乱

  17. 17

    Golangのgithub.com/google/google-api-go-client/customsearch/v1の使用方法

  18. 18

    API V1 から API V2 への Objective-C ドロップボックス SDK の移行

  19. 19

    FCM HTTP V1 API returns 404 for unregistered token

  20. 20

    FCM HTTP V1 API returns 404 for unregistered token

  21. 21

    How to sort response by coinmarketcap api v1 by price in usd?

  22. 22

    How to remove a PayPal product from v1/catalogs API?

  23. 23

    Payment issue with PayPal REST API v1

  24. 24

    Rails6-HerokuのInvalidAuthenticityToken

  25. 25

    / api / v1 / **へのすべてのリクエストを古いAPIサーバーに渡す

  26. 26

    ActionController :: InvalidAuthenticityToken Rails 5 / Devise / Audited / PaperTrail gem

  27. 27

    断続的なRails5 ActionController :: InvalidAuthenticityToken

  28. 28

    Rails4 ActionController :: InvalidAuthenticityTokenエラー

  29. 29

    Expressで/ api / v1 /(ここではどのルートでも)を使用する意味は何ですか?

ホットタグ

アーカイブ