質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

Q&A

0回答

1560閲覧

railsで作成したAPIをテストしているのですが、devise_token_authの認証関連でpostアクションでエラーが発生してしまい、対応に困っています。

ganbarou_nippon

総合スコア18

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

0グッド

0クリップ

投稿2020/04/15 07:53

現在webアプリに、API機能を追加しようとしているのですが、deviseとdevise_token_auth機能の併用を想定したもので、基本設定が完了し、Postmanというアプリでテストしようとしたところ、以下のようなエラーがずっと起きてしまい、対応に困っています。

関連ファイルは以下です。

ruby

1config/routes.rb 2Rails.application.routes.draw do 3 devise_for :users 4 namespace :apis do 5 namespace :api do 6 scope :v1 do 7 mount_devise_token_auth_for 'User', at: 'auth',controllers: {registrations:'api'} 8 resources :users,only:[:index,:show,:edit,:update] 9 resources :notes do 10 get :search, on: :collection 11 end 12 resources :comments,only:[:index,:create,:edit,:update,:destroy] 13 resources :replies,only:[:index,:create,:edit,:update,:destroy] 14 resources :likes,only:[:index,:create,:destroy] 15 end 16 end 17 (webアプリ部分なので中略) 18end

ruby

1app/controllers/api_controller.rb 2class ApiController < ActionController::API # Note: here is not ::BASE 3 include DeviseTokenAuth::Concerns::SetUserByToken 4 include ActionController::RequestForgeryProtection 5 protect_from_forgery with: :null_session 6 before_action :set_host # この行を追加 7 8 def create 9 end 10 11 def update 12 end 13 14 def destroy 15 end 16 17 def edit 18 end 19 20 def new 21 end 22 23 def cancel 24 end 25 26 def protect_from_forgery 27 end 28end

ruby

1config/initializers/devise_token_auth.rb 2 3# frozen_string_literal: true 4 5DeviseTokenAuth.setup do |config| 6(中略) 7 # Makes it possible to change the headers names 8 config.headers_names = {:'access-token' => 'access-token', 9 :'client' => 'client', 10 :'expiry' => 'expiry', 11 :'uid' => 'uid', 12 :'token-type' => 'token-type' } 13(中略) 14 config.enable_standard_devise_support = true 15 config.default_confirm_success_url = "confirmed"# この行を追加 16 17 config.change_headers_on_each_request = false 18 config.token_lifespan = 1.month 19(中略) 20end

ruby

1app/models/user.rb 2 3class User < ApplicationRecord 4 # Include default devise modules. 5 devise :database_authenticatable, :registerable, 6 :recoverable, :rememberable,:validatable, 7 :confirmable 8 include DeviseTokenAuth::Concerns::User 9 include DeviseTokenAuth::Concerns::ActiveRecordSupport 10 11 has_many :notes ,:dependent => :destroy 12 has_many :comments ,:dependent => :destroy 13 has_many :replies ,:dependent => :destroy 14 has_many :likes, :dependent => :destroy 15 validates :name, presence: true 16 validates :image, presence: true 17 validates :introduction, presence: true,length: {maximum:160} 18 mount_uploader :image, ImagesUploader 19 20 (中略) 21 after_create :send_confirmation_email, if: -> { !Rails.env.test? && User.devise_modules.include?(:confirmable) } 22 23 private 24 def send_confirmation_email 25 self.send_confirmation_instructions 26 end 27end

エラーで関連すると思われる部分は以下です。

2020-04-15T07:31:12.961343+00:00 heroku[router]: at=info method=POST path="/apis/api/v1/auth" host=afeaer.herokuapp.com request_id=1ac76586-8293-4aa0-872d-44bac5370484 fwd="106.154.136.149" dyno=web.1 connect=1ms service=19ms status=400 bytes=243 protocol=https 2020-04-15T07:31:12.948591+00:00 app[web.1]: [1ac76586-8293-4aa0-872d-44bac5370484] Started POST "/apis/api/v1/auth" for 106.154.136.149 at 2020-04-15 07:31:12 +0000 2020-04-15T07:31:12.951505+00:00 app[web.1]: [1ac76586-8293-4aa0-872d-44bac5370484] Error occurred while parsing request parameters. (中略) 2020-04-15T07:31:42.311942+00:00 app[web.1]: [ce629eeb-1bf0-4a7b-9cdd-d003bd08147f] ActionDispatch::Http::Parameters::ParseError (765: unexpected token at '------WebKitFormBoundarygS53MmHJwYYTyPZm

また、以下のURLに記載されている解決法は試しました。
https://teratail.com/questions/148827
https://qiita.com/ledsun/items/4e3431de080c97796817

どのように対応すれば良いでしょうか?
教えていただければ幸いです。
ご教授の方よろしくお願い申し上げます。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問