ruby on rails - Why does ActiveResource Post not Send Any Parameters? -
i trying create new "user" in mongodb/sinatra server rails3 client using activeresource , json , object body or hash sent empty. in rails3, created "user" model , using rails console make activeresource call sinatra server. server correctly reads url, no parameters seem passed object. can please give me thoughts have missed or why not getting correct output?
sinatra server uses ruby 1.8.7.
rails3 client uses ruby 1.9.2.
from rails3 client user model:
class user < activeresource::base self.site = "http://127.0.0.1:9393/" self.collection_name = "user/add" self.format = :json end
from rails3 console:
u=user.new(:first_name=>"bill",:last_name=>"smith") => #<user:0xa8d7fac @attributes={"first_name"=>"bill", "last_name"=>"smith"}, @prefix_options={}> u.save => true
the sinatra app server receives following object (which retrieve sinatra server using "puts @app.inspect"):
#<api:0xb6ee4e70 @block_params=[], @result={"status"=>200, "error"=>nil}, @params={}, @observer_state=true, @request=#<sinatra::request:0xb6ee4e0c @params={}, @route="/user/add.json", @env={"http_host"=>"127.0.0.1:9393", "http_accept"=>"*/*", "server_name"=>"127.0.0.1", "request_path"=>"/user/add.json", "rack.url_scheme"=>"http", "http_user_agent"=>"ruby", "remote_host"=>"localhost", "content_length"=>"50", "rack.errors"=>#<rack::lint::errorwrapper:0xb6ee5294 @error=#<io:0xb77e955c>>, "server_protocol"=>"http/1.1", "content_type"=>"application/json", "rack.version"=>[1, 1], "rack.run_once"=>false, "server_software"=>"webrick/1.3.1 (ruby/1.8.7/2011-02-18)", "remote_addr"=>"127.0.0.1", "path_info"=>"/user/add.json", "script_name"=>"", "http_version"=>"http/1.1", "rack.multithread"=>true, "rack.multiprocess"=>false, "request_uri"=>"http://127.0.0.1:9393/user/add.json", "rack.request.query_hash"=>{}, "server_port"=>"9393", "request_method"=>"post", "rack.request.query_string"=>"", "rack.input"=>#<rack::lint::inputwrapper:0xb6ee52d0 @input=#<stringio:0xb76fbeb0>>, "http_connection"=>"close", "query_string"=>"", "gateway_interface"=>"cgi/1.1"}>, @original_params={}, @template_cache=#<tilt::cache:0xb6ee4fb0 @cache={}>, @env={"http_host"=>"127.0.0.1:9393", "http_accept"=>"*/*", "server_name"=>"127.0.0.1", "request_path"=>"/user/add.json", "rack.url_scheme"=>"http", "http_user_agent"=>"ruby", "remote_host"=>"localhost", "content_length"=>"50", "rack.errors"=>#<rack::lint::errorwrapper:0xb6ee5294 @error=#<io:0xb77e955c>>, "server_protocol"=>"http/1.1", "content_type"=>"application/json", "rack.version"=>[1, 1], "rack.run_once"=>false, "server_software"=>"webrick/1.3.1 (ruby/1.8.7/2011-02-18)", "remote_addr"=>"127.0.0.1", "path_info"=>"/user/add.json", "script_name"=>"", "http_version"=>"http/1.1", "rack.multithread"=>true, "rack.multiprocess"=>false, "request_uri"=>"http://127.0.0.1:9393/user/add.json", "rack.request.query_hash"=>{}, "server_port"=>"9393", "request_method"=>"post", "rack.request.query_string"=>"", "rack.input"=>#<rack::lint::inputwrapper:0xb6ee52d0 @input=#<stringio:0xb76fbeb0>>, "http_connection"=>"close", "query_string"=>"", "gateway_interface"=>"cgi/1.1"}, @app=nil, @observer_peers=[#<hookmongo:0xb6d714f8 @extra=nil, @app=#<api:0xb6ee4e70 ...>>], @response=#<sinatra::response:0xb6edb94c @writer=#<proc:0xb77cd4b0@/home/scott/.rvm/gems/ruby-1.8.7-p334@api/gems/rack-1.3.0/lib/rack/response.rb:28>, @block=nil, @chunked=false, @length=0, @header={"content-type"=>nil}, @body=[], @status=200>>
as can see @params={} empty.
using http request works:
net::http.post_form(uri.parse('http://127.0.0.1:9393/user/add.json'),{'first_name' => 'smith', 'last_name' => 'bill'})
and produces this:
#<api:0xb6ee4fec @block_params=[], @result={"status"=>200, "error"=>nil}, @params={"last_name"=>"bill", "first_name"=>"smith"}, @observer_state=true, @request=#<sinatra::request:0xb6ee4f88 @params={"last_name"=>"bill", "first_name"=>"smith"}, @route="/user/add.json", @env={"http_host"=>"127.0.0.1:9393", "http_accept"=>"*/*", "server_name"=>"127.0.0.1", "request_path"=>"/user/add.json", "rack.url_scheme"=>"http", "http_user_agent"=>"ruby", "remote_host"=>"localhost", "content_length"=>"31", "rack.errors"=>#<rack::lint::errorwrapper:0xb6ee5410 @error=#<io:0xb77e955c>>, "server_protocol"=>"http/1.1", "content_type"=>"application/x-www-form-urlencoded", "rack.version"=>[1, 1], "rack.run_once"=>false, "server_software"=>"webrick/1.3.1 (ruby/1.8.7/2011-02-18)", "remote_addr"=>"127.0.0.1", "path_info"=>"/user/add.json", "script_name"=>"", "http_version"=>"http/1.1", "rack.multithread"=>true, "rack.request.form_vars"=>"first_name=smith&last_name=bill", "rack.multiprocess"=>false, "request_uri"=>"http://127.0.0.1:9393/user/add.json", "rack.request.form_input"=>#<rack::lint::inputwrapper:0xb6ee544c @input=#<stringio:0xb76d72a4>>, "rack.request.query_hash"=>{}, "server_port"=>"9393", "request_method"=>"post", "rack.request.form_hash"=>{"last_name"=>"bill", "first_name"=>"smith"}, "rack.request.query_string"=>"", "rack.input"=>#<rack::lint::inputwrapper:0xb6ee544c @input=#<stringio:0xb76d72a4>>, "query_string"=>"", "gateway_interface"=>"cgi/1.1"}>, @original_params={"last_name"=>"bill", "first_name"=>"smith"}, @template_cache=#<tilt::cache:0xb6ee512c @cache={}>, @env={"http_host"=>"127.0.0.1:9393", "http_accept"=>"*/*", "server_name"=>"127.0.0.1", "request_path"=>"/user/add.json", "rack.url_scheme"=>"http", "http_user_agent"=>"ruby", "remote_host"=>"localhost", "content_length"=>"31", "rack.errors"=>#<rack::lint::errorwrapper:0xb6ee5410 @error=#<io:0xb77e955c>>, "server_protocol"=>"http/1.1", "content_type"=>"application/x-www-form-urlencoded", "rack.version"=>[1, 1], "rack.run_once"=>false, "server_software"=>"webrick/1.3.1 (ruby/1.8.7/2011-02-18)", "remote_addr"=>"127.0.0.1", "path_info"=>"/user/add.json", "script_name"=>"", "http_version"=>"http/1.1", "rack.multithread"=>true, "rack.request.form_vars"=>"first_name=smith&last_name=bill", "rack.multiprocess"=>false, "request_uri"=>"http://127.0.0.1:9393/user/add.json", "rack.request.form_input"=>#<rack::lint::inputwrapper:0xb6ee544c @input=#<stringio:0xb76d72a4>>, "rack.request.query_hash"=>{}, "server_port"=>"9393", "request_method"=>"post", "rack.request.form_hash"=>{"last_name"=>"bill", "first_name"=>"smith"}, "rack.request.query_string"=>"", "rack.input"=>#<rack::lint::inputwrapper:0xb6ee544c @input=#<stringio:0xb76d72a4>>, "query_string"=>"", "gateway_interface"=>"cgi/1.1"}, @app=nil, @observer_peers=[#<hookmongo:0xb6d68c18 @extra=nil, @app=#<api:0xb6ee4fec ...>>], @response=#<sinatra::response:0xb6edbac8 @writer=#<proc:0xb77cd4b0@/home/scott/.rvm/gems/ruby-1.8.7-p334@api/gems/rack-1.3.0/lib/rack/response.rb:28>, @block=nil, @chunked=false, @length=0, @header={"content-type"=>nil}, @body=[], @status=200>>
@params={"last_name"=>"bill", "first_name"=>"smith"} no longer empty.
in active_resource/connection.rb file, line 94
# executes post request. # used create new resources. def post(path, body = '', headers = {}) with_auth { request(:post, path, body.to_s, build_request_headers(headers, :post, self.site.merge(path))) } end
i'm sure i'm missing something, seems create empty body.
thanks advice in advance.
sinatra doesn't parse body default. if there form encoding header, rack parse form encoded body , put in params why using post_form
works. need pull request body out , parse yourself. e.g.
post '/add' user_hash = json.parse request.body user.create! user_hash end
Comments
Post a Comment