我似乎无法向我的控制器端点发出帖子请求;但是,我可以使用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的问题,但我认为不是。香港专业教育学院尝试了新的铁路5 api和相同。
我的js函数看起来像:
axios.post(`${window.apiUrl}/v1/users/test`, { headers: headerData, project: { title: 'help' } })
答案 0 :(得分:0)
事实证明,这是我用axios发布数据的方式。我应该在标题之前有数据:
axios.post('url', <data-to-post>, <headers>)
相反,我只发送了params
而不是headers
。问题解决了。