如何使用从RoR应用程序到另一个RoR应用程序的HTTP POST请求来处理AuthenticityToken值?
我正在使用Ruby on Rails 3,我想知道如何使用从RoR应用程序到另一个RoR应用程序的HTTP POST请求来处理AuthenticityToken值。在这种情况下,我的目的是提交一个登录表格,如果他提供了正确的
email
和password
值,则以JSON格式返回用户信息。
我在这个URL上有一个RoR应用程序
pjtnam.com
和此URL上的另一个RoR应用程序
users.pjtname.com
如果我从应用程序pjtname.com
向应用程序users.pjtname.com
发出HTTP POST请求(在本例中我使用了Typhoeus gem)
response = Typhoeus::Request.post("http://users.pjtname.com/authentications",
:params => {
:new_authentication => {
:email => email,
:password => password
}
}
)
我收到了这个回复
<h1>
ActionController::InvalidAuthenticityToken
in AuthenticationsController#create
</h1>
<pre>ActionController::InvalidAuthenticityToken</pre>
那么,如何在安全方法模式中处理AuthenticityToken值?我想知道应用程序何时位于同一服务器上以及何时不存在。
在http://users.pjtname.com/authentications/new
我有以下用于登录用户的表格:
<%= form_for(:new_authentication) do |f| %>
<%= f.label :email %>
<%= f.label :password %>
<%= f.submit "Sign in" %>
<% end %>
在authentications_controller.rb我有
def create
# Note ':authentication' symbol is different than ':new_authentication' seen in HTTP POST parameters and in the above form
@authentication = Authentication.new(params[:authentication])
@account = Account.sign_in_account(params[:new_authentication][:email], params[:new_authentication][:password])
...
respond_to do |format|
format.html {
redirect_to @account
}
format.js {
render(:update) { |page|
page.redirect_to @account
}
}
format.json {
render :json => @account
}
end
end
在routes.rb我有
resources :authentications do #, :path => "authentication" do
member do
get 'confirm_authentication'
post 'confirm_authentication'
end
end
更新@idlefingers的答案
请求
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
}
)
响应
<h1>
StandardError
</h1>
<pre>Invalid JSON string</pre>
请求
Typhoeus::Request.post("http://users.pjtname.com/authentications/new.json",
:params => { ... } # Same parameters as above
}
)
响应
<h1>Routing Error</h1>
<p><pre>No route matches "/authentications/new.json"</pre></p>
没有找到相关结果
已邀请:
1 个回复
驮帽俺篮号