2012-01-11 104 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