Friday, 15 January 2010

rest - How to insert into RAILS db via external RESTful call -



rest - How to insert into RAILS db via external RESTful call -

solved: had done few things wrong, of involved controller receiving data. there not wrong methods below on sending data.

1: not using @report.save in reportcontroller#create

2: not passing params[:report] in controller

3: added "skip_before_filter :verify_authenticity_token" applicaiton controller stop warnings in logs. solved. info insertion successful.

=====orig. question below=====

i need external programme issue command inserts stuff ruby on rails database. understand security implications of this, because application not public facing, not issue.

this workflow looking achieve:

rest client > rails > create new db table row

for purposes of example: route.rb file contains

resources :reports

so able crud using routes. cant seem rest client work correctly.

update: have tried ruby rest client , curl command in one, no avail.

require 'rest_client' require 'json' hash_to_send = {:test_name => 'fake name', :pass_fail => 'pass',:run_id => 1111, :category => 'fake category'} #formulate curl effort mycommand = "curl -h 'content-type: application/json' -x post http://localhost:8889/report.json -d #{hash_to_send.to_json} > deleteme.html" #execute curl effort `#{mycommand}` # result--> 795: unexpected token @ 'test_name:fake name' #make ruby rest client effort response = restclient.post( "http://localhost:8889/report.json", hash_to_send.to_json, :content_type => :json, :accept => :json ) #debug info puts mycommand # returns --> {"test_name":"fake name","pass_fail":"pass","run_id":1111,"category":"fake category"}

instead of curl in command-line, utilize ruby script , handle rest calls , json conversion gems. example, using rest-client gem (https://github.com/archiloque/rest-client) , standard json gem can write:

require 'rest_client' require 'json' response = restclient.post( "http://localhost:8889/report.json", params_in_hash.to_json, { :content_type => :json, :accept => :json } )

ruby-on-rails rest curl

No comments:

Post a Comment