Rest-Client获得authenticity_token来创建新记录 [英] Rest-Client get authenticity_token to create new record
问题描述
我尝试从ruby程序中创建一个新人,为此任务,我使用rest-client:
I try to create an new person out of an ruby program, for this task i use rest-client:
require 'rest_client'
require 'nokogiri'
page = Nokogiri::HTML(RestClient.get("localhost:3000/people/new"))
token = page.css("input[name='authenticity_token']")
token2 = token[0]["value"]
RestClient.post 'localhost:3000/people', :authenticity_token => token2, :person => {:name => 'Joseph'}, :commit => 'Create Person'
我试图通过首先向people/new发出get请求并从nokogiri的隐藏输入中读取出authenticity_token来获取一个authenticity_token:
I tried to get an authenticity_token by first make an get request to people/new and read out the authenticity_token from the hidden input with nokogiri:
<input name="authenticity_token" type="hidden" value="JKrWBtxcloak2DHucqHtTFZC6W7QyDJoJQI3QtCFBy8=">
然后,我使用此authenticity_token发出发帖请求,如上所示.
Then i make an post request with this authenticity_token, how you can see above.
但是这在某些情况下不起作用,我的Rails控制台说它没有令牌的真实性:
But somehwo this wont work, my rails console says it has no token authenticity:
Started POST "/people" for 127.0.0.1 at 2013-09-09 09:33:27 +0200
Processing by PeopleController#create as XML
Parameters: {"authenticity_token"=>"WnbZY/060H5IMGdpVMyRKwG/CHKNAEGafOV3i1f8Kj
o=", "person"=>{"name"=>"Joseph"}, "commit"=>"Create Person"}
WARNING: Can't verify CSRF token authenticity
我使用虚假的真实性令牌还是我错了?
Do i use an false authenticity token or what do i wrong?
更新::::::::::::::::::::::::::::::::::::::::::::::: ::::::
UPDATE::::::::::::::::::::::::::::::::::::::::::::::::::::
我写了一个新程序,得到一个set-cookie:但是,如何在我的参数中分配cookie?
I wrote an new programm were i get an set-cookie: But how do i have assign the cookie in my params?
require 'rest_client'
response = RestClient.get("localhost:3000/people/new")
puts response.headers
输出
{:content_type=>"text/html; charset=utf-8", :x_ua_compatible=>"IE=Edge", :etag=>
"\"aeb1c39272f16046456cf66c53a9ee7c\"", :cache_control=>"max-age=0, private, mus
t-revalidate", :x_request_id=>"87d76be013cb156113b15c11875edeaa", :x_runtime=>"0
.025002", :server=>"WEBrick/1.3.1 (Ruby/1.9.3/2013-02-22)", :date=>"Mon, 09 Sep
2013 09:08:36 GMT", :content_length=>"1407", :connection=>"Keep-Alive", :set_coo
kie=>["_rumba_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJTBjMWI3YTkzZjI0MjZmZmE4Zm
QwMzI4ZGE1NmRhNWU3BjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMWxqemZjUUMyNG81T2diZjFRN2ZNZn
ljMFNmalp4NUtLcXZhcnVocWcxQWc9BjsARg%3D%3D--3439371de3cba7c2352eace74051858dcc13
7333; path=/; HttpOnly"]}
推荐答案
可能您还必须发送
<input name="utf8" type="hidden" value="✓">
这篇关于Rest-Client获得authenticity_token来创建新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!