如何讓 Ruby on Rails 與 ASIHTTPRequest 一起使用? (How do I get Ruby on Rails to work with ASIHTTPRequest?)


問題描述

如何讓 Ruby on Rails 與 ASIHTTPRequest 一起使用? (How do I get Ruby on Rails to work with ASIHTTPRequest?)

I am creating an iOS application with a Ruby on Rails backend. I've got my logic setup and working in Rails, and have verified by testing in a web browser. My Rails application will not respond properly to my iOS application, saying something about an authenticity token.

I have setup an authenticity token in application.rb with the following code (I did a server reboot after adding this):

protect_from_forgery :secret => 'some_secret_here'

I'm passing the authenticity token from iOS to Rails with ASIHTTPRequest using the following code:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"some_secret_here" forKey:@"authenticity_token"];
[request setPostValue:@"some value" forKey:@"some_parameter"];
[request setDelegate:self];
[request startAsynchronous];

Is there anything I'm missing or doing wrong?

This same code works fine with allow_forgery_protection set to false, so I assume the error lies in how I'm trying to pass the authenticity token.


參考解法

方法 1:

The authenticity token is different for each page/request, so you need to find a way to send it through the pipe some other way.

Maybe try sending it via headers, like so:

class ApplicationController < ActionController::Base
    before_filter :send_form_authenticity_token

    private
      def send_form_authenticity_token
        response.headers['X-Authenticity-Token'] = form_authenticity_token
      end
end

On the response callback of your first (and following) request(s), you need to do:

NSString *authenticityToken = [[request responseHeaders] objectForKey:@"X-Authenticity-Token"];

This implies that the first request you do is a GET one. 

Would be better to have a global state variable, but this is the basic concept. 

By the way, and just so you know, you don't need forgery protection if your rails application is just a backend sorts of app.

Forgery protection is there to avoid XSS attacks, and that wouldn't happen on the iPhone.

(by Jeremy Whitechangelog)

參考文件

  1. How do I get Ruby on Rails to work with ASIHTTPRequest? (CC BY-SA 3.0/4.0)

#asihttprequest #objective-c #iphone #iOS #ruby-on-rails






相關問題

遠程服務器上的請求總是失敗 (Request always fail on distant server)

ASIHTTPRequest 緩存我的數據。我不想要任何類型的緩存 (ASIHTTPRequest caches my data. I don't want any kind of cache)

在調用之前釋放了 didFinishSelector ASIHTTPRequest 的自定義委託類 (Custom delegate class for didFinishSelector ASIHTTPRequest being deallocated before called)

Android:在後台上傳/下載文件數量的最佳方式 (Android: best way to upload/download number of files in background)

Ghi giá trị bài đăng trong ASIFormDataRequest (Log Post Value in ASIFormDataRequest)

ASIHTTPRequest - 下載問題 (ASIHTTPRequest - download problem)

ASIHTTPRequest 離線模式連接失敗 (ASIHTTPRequest offline mode connection failure)

ASIHttpRequest 請求:didReceiveBytes:未按預期工作 (ASIHttpRequest request:didReceiveBytes: not working as expected)

如何讓 Ruby on Rails 與 ASIHTTPRequest 一起使用? (How do I get Ruby on Rails to work with ASIHTTPRequest?)

可以指定一個 NSObject 作為多個 ASIHTTPRequests 的代表嗎? (OK to assign one NSObject as the delegate of multiple ASIHTTPRequests?)

需要幫助將 NSXMLParser 與 ASIHTTPRequest 一起使用 (need help using NSXMLParser with ASIHTTPRequest)

處理 ASIHTTPRequest 多個請求 (Handling ASIHTTPRequest Multiple Requests)







留言討論