2012-01-11 103 views
0

我在與Restkit +的iOS 5 + Rails的一個奇怪的問題軌發帖時:當我嘗試做這樣的服務器上的一個帖子:錯誤404與RestKit的iOS 5

NSArray *topicsList=[self.topicsTV.text componentsSeparatedByString:@","]; 
RKParams *params = [NSDictionary dictionaryWithObjectsAndKeys: self.questionTV.text,@"question[text]", 
                   self.descriptionTV.text,@"question[detail]",      
                   topicsList,@"topics[]", nil 
        ]; 
[[RKClient sharedClient] post:@"/questions.json" params:params delegate:self]; 

日誌會是這樣:

2012-01-11 17:24:21.725 APP[29087:fb03] I restkit.network:RKRequest.m:562 Status Code: 401 
2012-01-11 17:24:21.725 APP[29087:fb03] I restkit.network:RKRequest.m:563 Body: {"error":"You have to register or login."} 

注意張貼我在100%記錄,因爲我能得到獲得一些私人的內容和所發生的事情是,如果我刷新私有內容(被髮送到服務器),它給前我這個錯誤:

2012-01-11 17:35:51.337 APP[29087:fb03] D restkit.network.queue:RKRequestQueue.m:455 Request <RKObjectLoader: 0x811c360> failed loading in queue <RKRequestQueue: 0xc60ea10 name=(null) suspended=NO requestCount=0 loadingCount=0/5> with error: The operation couldn’t be completed. (org.restkit.RestKit.ErrorDomain error 1004.).(Now loading 0 of 5). 

該操作是否註銷了我?我應該如何維持登錄會話活着?

+0

theese是我的職位參數: 參數:{ 「UTF8」=> 「✓」, 「authenticity_token」=> 「rEwYQT9tKGtqW4Tp4wtBZTN5zl + HPOK5k/ZpGs0nAJc =」, 「問題」=> {「text」=>「Prova prova prova」,「detail」=>「」},「suggested-query-input」=>「」,「topics」=> [「argomento」,「nuovo」],「variables 「=>」prova-prova-prova-1「,」commit「=>」Salva「,」a「=>」questions「} – Massimo 2012-01-12 10:59:05

+0

基本上我想這可能是一個csrf,我不保留...任何理念? – Massimo 2012-01-16 09:19:37

+0

現在的問題是我必須修改我的iOS應用程序或rails應用程序中的somethig嗎? – Massimo 2012-01-16 11:35:46

回答

0

可能發生的情況是,當應用程序運行POST請求時,rails應用程序無法驗證CSRF令牌。當你運行GET請求時,它不需要檢查令牌(因爲GET請求不會改變任何東西)。這就是爲什麼你可以用用戶名和密碼來獲取某些東西,但不能發佈一些東西。這blog post有一些更多的信息。

我發現的幫助是禁用了任何JSON請求的檢查。

# In your application_controller.rb 
skip_before_filter :verify_authenticity_token, :if => Proc.new { |c| 
    c.request.format == 'application/json' } 

當然,你也可以這樣做:

# Or this in your application_controller.rb 
def verified_request? 
    if request.content_type == "application/json" 
    true 
    else 
    super() 
    end 
end 

無論是哪種允許JSON請求忽略令牌,並且可以適當CRUD操作。只要你選擇忽略檢查,就知道你在做什麼。

另外:https://github.com/rails/rails/issues/3041