簡體   English   中英

Rails無法驗證CSRF令牌的真實性

[英]Rails Can't verify CSRF token authenticity

我已經通過使用像這樣的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

我也用以下命令關閉了csrfprotect_from_forgery protect_from_forgery with: :null_session

是否有可能使用Curl::PostField自動包含csrf token或者是否有任何類似的方法,例如patch or put method Curl::Postfield

您可以嘗試顯式跳過令牌過濾器。

在您的API控制器中:

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

確保也重新啟動服務器。 希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM