I am trying to create a new "User" in a MongoDB/Sinatra Server from a Rails3 Client using ActiveResource and Json and the object body or hash that is sent is empty. In Rails3, I created a "User" model and using the Rails Console I make an ActiveResource call to the Sinatra Server. The Server correctly reads the URL, but no parameters seem to get passed with the object. Can someone please give me their thoughts as to what I have missed or why I am not getting the 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 the 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 the following object (which I retrieve from the 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 you can see the @params={} is empty.
Using the 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.reques开发者_如何学Ct.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"} is no longer empty.
In the active_resource/connection.rb file, line 94
# Executes a POST request.
# Used to 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, but this seems to create a empty body.
Thanks for your advice in advance.
Sinatra doesn't parse the body by default. If there is a form encoding header, Rack will parse the form encoded body and put it in the params which is why using post_form
works. You need to pull the request body out and parse it yourself. e.g.
post '/add' do
user_hash = JSON.parse request.body
User.create! user_hash
end
精彩评论