【发布时间】:2016-05-12 21:40:42
【问题描述】:
我想将POST request 发送给我的本地开发人员,如下所示:
HTTParty.post('http://localhost:3000/fetch_heroku',
:body => {:type => 'product'},)
但是,它会从服务器控制台报告
Started POST "/fetch_heroku" for 127.0.0.1 at 2016-02-03 23:33:39 +0800
ActiveRecord::SchemaMigration Load (0.0ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by AdminController#fetch_heroku as */*
Parameters: {"type"=>"product"}
Can't verify CSRF token authenticity
Completed 422 Unprocessable Entity in 1ms
这是我的控制器和路由设置,非常简单。
def fetch_heroku
if params[:type] == 'product'
flash[:alert] = 'Fetch Product From Heroku'
Heroku.get_product
end
end
post 'fetch_heroku' => 'admin#fetch_heroku'
我不确定我需要做什么?关闭 CSRF 肯定会起作用,但我认为创建这样的 API 时应该是我的错误。
我还需要做其他设置吗?
【问题讨论】:
-
对于 API,通常接受关闭 CSRF 令牌验证。我用
protect_from_forgery with: :null_session。
标签: ruby-on-rails