为什么ActiveResource Post不发送任何参数?

时间:2011-06-30 18:02:05

标签: ruby-on-rails sinatra activeresource

我正在尝试使用ActiveResource和Json从Rails3客户端在MongoDB / Sinatra服务器中创建一个新的“User”,并且发送的对象主体或散列是空的。在Rails3中,我创建了一个“用户”模型并使用Rails控制台对Sinatra Server进行了ActiveResource调用。服务器正确读取URL,但似乎没有参数传递给对象。有人可以请你告诉我我错过了什么,或者为什么我没有得到正确的输出?

Sinatra Server使用Ruby 1.8.7 Rails3客户端使用Ruby 1.9.2。

来自Rails3客户端用户模型:

class User < ActiveResource::Base 
   self.site = "http://127.0.0.1:9393/"
   self.collection_name = "user/add"
   self.format = :json 
end

从Rails3控制台:

u=User.new(:first_name=>"Bill",:last_name=>"Smith")
=> #<User:0xa8d7fac @attributes={"first_name"=>"Bill", "last_name"=>"Smith"}, @prefix_options={}>
u.save
=> True

Sinatra app服务器接收以下对象(我使用“puts @ app.inspect”从Sinatra服务器检索):

 #<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>>

正如您所见,@ params = {}为空。

使用HTTP请求有效:

Net::HTTP.post_form(URI.parse('http://127.0.0.1:9393/user/add.json'),{'first_name' => 'Smith', 'last_name' => 'Bill'})

并产生这个:

#<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”=&gt;“Bill”,“first_name”=&gt;“Smith”}不再为空。

在active_resource / connection.rb文件中,第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

我确定我错过了一些东西,但这似乎创造了一个空洞的身体。

提前感谢您的建议。

1 个答案:

答案 0 :(得分:3)

Sinatra默认不解析正文。如果有一个表单编码头,Rack将解析表单编码的主体并将其放在params中,这就是使用post_form工作的原因。您需要拉出请求主体并自己解析它。 e.g。

post '/add' do
  user_hash = JSON.parse request.body
  User.create! user_hash
end