2017-02-24 84 views
1

我已经成功地通过使用POST方法API这样创造了新的纪录(使用Curl::PostField导轨无法验证CSRF令牌真实性

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还是有像patch or put methodCurl::Postfield任何类似的方式关闭?

+0

嗨,大家好,我仍然坚持这一点,请帮忙吗? –

回答

-1

你可以尝试跳过明确令牌过滤器。

在您的API控制器:

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

确保重新启动服务器上。希望这可以帮助!

+1

这是否使API不太安全? –

+0

我想知道还有另一种方式,而不必关闭csrf或跳过令牌验证? –

+0

您可以添加自己的api_key并在应用程序控制器中要求它。这是我前几天遇到的一个常见问题,我认为它与Rails 5有关。您是否正在运行仅API应用程序? –

相关问题