获取401上的PATCH请求与AFNetworking / RestKit一起发送,但202从浏览器发送

时间:2012-12-26 22:59:18

标签: ios django restkit afnetworking tastypie

所以我使用Tastypie在我的Django服务器上运行这个RESTful API。 所以我从javascript做了以下代码,一切正常。

       var newData = {};
       newData["content"] = "This is a new test";
       $.ajax({ 
           type: 'patch',
           contentType: "application/json; charset=utf-8",
           url: "/api/rest/sopsteps/17/",
           data: JSON.stringify(newData),
           dataType: "json",                    
           success: function(data, status) {
               alert("success");
           },
           error: function(data, status){
               alert(data + " STATUS : " + status);
           },
       });                

现在,如果我使用AFNetworking / RestKit执行以下操作,我总会得到401。

[26 / Dec / 2012 16:20:20]“PATCH / api / rest / sopsteps / 17 / HTTP / 1.1”401 0

NSDictionary* params = [[NSDictionary alloc] initWithObjectsAndKeys: _contentTextView.text, @"content", nil];



NSString *path = [NSString stringWithFormat:@"/api/rest/sopsteps/%@/",((PokaSOPStep *)_step).identifier];


[[objectManager HTTPClient]patchPath:path parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject)
 {
     _step.content = _contentTextView.text;
     NSLog(@"%@", operation.responseString);
 }
                            failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {
     if(operation.responseData)
     {
         id json = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
         if(!json)
         {
             NSLog(@"An unexpected error occurred. No JSON from Server");
         }
         else
         {
             NSLog(@"An unexpected error occurred.");
         }
     }
     else
     {
         NSLog(@"The Server is currently not responding.");
     }

 }];

但是如果我从浏览器中执行此操作,我会得到:

[26 / Dec / 2012 16:54:57]“PATCH / api / rest / sopsteps / 17 / HTTP / 1.1”202 0

任何想法?!谢谢!

1 个答案:

答案 0 :(得分:0)

401是“未授权”的HTTP代码。您在浏览器和AFNetworking中获得不同结果的原因是您已登录前者,而不是后者(您可以通过在其他浏览器中尝试此操作来检查)。

我不知道您为服务器设置了哪种身份验证方案,但是在您能够提出请求之前,您需要在iOS上登录。