Rest-Client get authenticity_token to create new record
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'
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=">
Then i make an post request with this authenticity_token, how you can see
above.
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?
No comments:
Post a Comment