【问题标题】:Cannot make post request using devise token auth无法使用设计令牌身份验证发出发布请求
【发布时间】:2017-05-29 13:38:45
【问题描述】:

我似乎无法向我的控制器端点发出发布请求;但是,我可以使用devise_token_auth 创建一个用户和get 所有用户。

通过遵循他们的文档,我可以对用户进行 CRUD。如果我在带有post 路由的用户控制器中创建另一个端点,我会得到:

过滤器链停止为 :authenticate_user!呈现或重定向

但是get 有效。我的application.rb

config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'
    resource '*',
      :headers => :any,
      :expose  => ['access-token', 'expiry', 'token-type', 'uid', 'client'],
      :methods => [:get, :post, :delete, :patch, :options]
  end
end

用户 CRUD 仅适用于 their 路径端点,但不适用于我的。

# controllers/api/v1/api_controller.rb
module Api::V1
  class ApiController < ApplicationController
    before_action :authenticate_user!
    respond_to :json
  end
end

# controllers/api/v1/users_controller.rb
module Api::V1
  class UsersController < ApiController

    def create
      render json: 'created'
    end

  end
end

# routes
scope module: 'api' do
  namespace :v1 do
    post '/users/test', to: 'users#create'    # not authenticated!
    get '/users/test', to: 'users#create'     # authenticated.. works
    # resources :users, only: [:index, :show] # no issues here
  end
end

当我添加标题但不是从浏览器添加时,邮递员可以post。不确定CORS的问题,但我认为不是。我尝试了一个新的 rails 5 api 和相同的。 我的 js 函数看起来像:

axios.post(`${window.apiUrl}/v1/users/test`, { headers: headerData, project: { title: 'help' } })

【问题讨论】:

    标签: ruby-on-rails devise vue.js ruby-on-rails-5 axios


    【解决方案1】:

    原来这就是我用 axios 发布数据的方式。我应该在标题之前有数据:

    axios.post('url', <data-to-post>, <headers>)
    

    相反,我只发送params 而不是headers。问题解决了。

    【讨论】:

      猜你喜欢
      • 2018-11-12
      • 1970-01-01
      • 2016-09-26
      • 2018-07-22
      • 2016-07-31
      • 1970-01-01
      • 2017-01-01
      • 2015-11-04
      • 2020-11-04
      相关资源
      最近更新 更多