現在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
どのように対応すれば良いでしょうか?
教えていただければ幸いです。
ご教授の方よろしくお願い申し上げます。
あなたの回答
tips
プレビュー