Ruby on rails 如何使用从RoR应用程序到另一个RoR应用程序的HTTP POST请求处理AuthenticityToken值?
我使用的是RubyonRails3,我想知道如何使用HTTP POST请求从一个RoR应用程序到另一个RoR应用程序来处理AuthenticityToken值。在这种情况下,如果用户提供了正确的Ruby on rails 如何使用从RoR应用程序到另一个RoR应用程序的HTTP POST请求处理AuthenticityToken值?,ruby-on-rails,ruby,http,ruby-on-rails-3,authenticity-token,Ruby On Rails,Ruby,Http,Ruby On Rails 3,Authenticity Token,我使用的是RubyonRails3,我想知道如何使用HTTP POST请求从一个RoR应用程序到另一个RoR应用程序来处理AuthenticityToken值。在这种情况下,如果用户提供了正确的email和password值,我将提交登录表单并以JSON格式返回用户信息 我在这个URL上有一个RoR应用程序 pjtnam.com users.pjtname.com 以及此URL上的另一个RoR应用程序 pjtnam.com users.pjtname.com 如果我像这样从应用程序pjt
email
和password
值,我将提交登录表单并以JSON格式返回用户信息
我在这个URL上有一个RoR应用程序
pjtnam.com
users.pjtname.com
以及此URL上的另一个RoR应用程序
pjtnam.com
users.pjtname.com
如果我像这样从应用程序pjtname.com
向应用程序users.pjtname.com
发出HTTP POST请求(在本例中,我使用
我得到了这样的回应
<h1>
ActionController::InvalidAuthenticityToken
in AuthenticationsController#create
</h1>
<pre>ActionController::InvalidAuthenticityToken</pre>
在authentications_controller.rb中,我有
resources :authentications do #, :path => "authentication" do
member do
get 'confirm_authentication'
post 'confirm_authentication'
end
end
Typhoeus::Request.post("http://users.pjtname.com/authentications/new",
# or
# Typhoeus::Request.post("http://users.pjtname.com/authentications",
:headers => {"Content-Type" => "application/json"},
:params => { ... } # Same parameters as above
}
)
在routes.rb中,我有
resources :authentications do #, :path => "authentication" do
member do
get 'confirm_authentication'
post 'confirm_authentication'
end
end
Typhoeus::Request.post("http://users.pjtname.com/authentications/new",
# or
# Typhoeus::Request.post("http://users.pjtname.com/authentications",
:headers => {"Content-Type" => "application/json"},
:params => { ... } # Same parameters as above
}
)
更新@idlefingers答案
请求
<h1>
StandardError
</h1>
<pre>Invalid JSON string</pre>
回应
Typhoeus::Request.post("http://users.pjtname.com/authentications/new.json",
:params => { ... } # Same parameters as above
}
)
回应
Typhoeus::Request.post("http://users.pjtname.com/authentications/new.json",
:params => { ... } # Same parameters as above
}
)
路由错误
没有与“/authentications/new.json”匹配的路由
它似乎没有以正确的内容类型发送请求。如果内容类型是application/xml或application/json,Rails应该跳过真实性令牌检查,这样就可以很好地使用api,而不必完全禁用真实性令牌
我不知道Typhous gem,但看起来您可能需要将“.json”或“.xml”添加到url(取决于您实现的API),或者可能需要将其作为标题哈希中的选项传入。这两台主机运行在同一台服务器上吗?@Dex我更新了问题(在底部)为了回答您的问题,我更新了问题的更多信息:表单视图和控制器代码,一些测试结果。您可以添加路由吗?假设您使用的是
resources:authentications
,它希望帖子转到/authentications.json,而不是/authentications/new.json。您还需要设置能够接受json的操作(使用respond\u with
或respond\u to
-请参阅)问题在于发送参数。如果我使用这个typhous::Request.post(“http://users.pjtname.com/authentications.json?new_authentication[电邮]=test@test.ts&new_authentication[password]=aaaaaa“
它可以工作,但是如何使用typhous语法发送参数:params=>{:new_authentication=>{:email=>email,:password=>password}
?我刚刚就上述评论提出了一个新问题: