Rails无法验证CSRF令牌的真实性

时间:2017-02-24 10:45:49

标签: ruby-on-rails curl csrf csrf-protection curb

我通过使用这样的post方法api成功创建了新记录(使用Curl::PostField

curl_post_fields = [
    Curl::PostField.content('first_name', first_name),
    Curl::PostField.content('last_name', last_name),]

  contact = Curl::Easy.http_post("#{ENV['API_V1_URL']}/contacts", *curl_post_fields)
  contact.ssl_verify_peer = false
  contact.http_auth_types = :basic
  contact.username = ENV['API_V1_USERNAME']
  contact.password = ENV['API_V1_PASSWORD']
  contact.perform
  response = JSON.parse(contact.body_str)

但是当我通过像这样的补丁更新记录时(使用json请求参数):

contact = Curl.patch("#{ENV['API_V1_URL']}/contacts/#{qontak_id}",{:email => email, :gender_id => 8})
contact.ssl_verify_peer = false
contact.http_auth_types = :basic
contact.username = ENV['API_V1_USERNAME']
contact.password = ENV['API_V1_PASSWORD']
contact.perform
response = JSON.parse(contact.body_str)

我从服务器

收到了一些错误
Can't verify CSRF token authenticity

Filter chain halted as :authenticate rendered or redirected

我也关掉了csrf protect_from_forgery with: :null_session

使用Curl::PostField可能会自动包含csrf token,还是有Curl::Postfield patch or put method中的 {"left":82.5,"top":48.875,"width":660,"height":371.25} 类似的方式?

1 个答案:

答案 0 :(得分:-1)

您可以尝试显式跳过令牌过滤器。

在您的API控制器中:

skip_before_filter :verify_authenticity_token, only: [:put] respond_to :json

确保也重启服务器。希望这有帮助!