ruby on rails - How to handle the AuthenticityToken value using a HTTP POST request from a RoR application to another RoR application? -
i using ruby on rails 3 , know how handle authenticitytoken value using http post request ror application ror application. in case aim submit sign in form , return user information in json format if he\she provided correct email
, password
values.
i have ror application @ url
pjtnam.com
and ror application @ url
users.pjtname.com
if make http post request application pjtname.com
application users.pjtname.com
(in example use typhoeus gem)
response = typhoeus::request.post("http://users.pjtname.com/authentications", :params => { :new_authentication => { :email => email, :password => password } } )
i response
<h1> actioncontroller::invalidauthenticitytoken in authenticationscontroller#create </h1> <pre>actioncontroller::invalidauthenticitytoken</pre>
so, how handle authenticitytoken value in safe approach\mode? i know in both when applications located on same server , when aren't.
at http://users.pjtname.com/authentications/new
have following form signing in users:
<%= form_for(:new_authentication) |f| %> <%= f.label :email %> <%= f.label :password %> <%= f.submit "sign in" %> <% end %>
in authentications_controller.rb have
def create # note ':authentication' symbol different ':new_authentication' seen in http post parameters , in above form @authentication = authentication.new(params[:authentication]) @account = account.sign_in_account(params[:new_authentication][:email], params[:new_authentication][:password]) ... respond_to |format| format.html { redirect_to @account } format.js { render(:update) { |page| page.redirect_to @account } } format.json { render :json => @account } end end
in routes.rb have
resources :authentications #, :path => "authentication" member 'confirm_authentication' post 'confirm_authentication' end end
update @idlefingers answer
request
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 above } )
response
<h1> standarderror </h1> <pre>invalid json string</pre>
request
typhoeus::request.post("http://users.pjtname.com/authentications/new.json", :params => { ... } # same parameters above } )
response
<h1>routing error</h1> <p><pre>no route matches "/authentications/new.json"</pre></p>
it looks it's not sending request correct content type. rails should skip authenticity token check if content-type application/xml or application/json, plays nice apis without having disable authenticity token altogether.
i don't know typhoeus gem, looks may need add ".json" or ".xml" url (depending on api you've implemented), or may need pass in options in headers hash.
Comments
Post a Comment