具有基本身份验证和特定API路径的Restkit

时间:2013-12-21 17:47:08

标签: ios objective-c rest restkit basic-authentication

我有一个关于restkit和基本身份验证的奇怪问题。 我想使用基本身份验证调用API服务器:

NSURL *baseURL = [NSURL URLWithString:@"http://myHost:8000/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
[httpClient setParameterEncoding:AFFormURLParameterEncoding];
[httpClient setDefaultHeader:@"Accept" value:@"application/json"];
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:httpClient];

// Setup the object mappings
        RKObjectMapping *userMapping = .....
//

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:theMapping
         method:RKRequestMethodGET
         pathPattern:@"/api/test"
         keyPath:nil
         statusCodes:[NSIndexSet indexSetWithIndex:200]];
        [objectManager addResponseDescriptor:responseDescriptor];


- (void) testAPI {
    RKObjectManager *objectManager = [RKObjectManager sharedManager];

    [objectManager.HTTPClient setAuthorizationHeaderWithUsername:@"test" password:@"test"];
    [objectManager getObjectsAtPath:@"/api/test"
                         parameters:nil
                            success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                /// Success

                            }
                            failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                /// Failure
                            }];

}

使用此代码,我有关于BasicAuth的错误:

E restkit.network:RKObjectRequestOperation.m:576 Object request failed: Underlying HTTP request operation failed with error: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1011 "Expected status code in (200), got 401" UserInfo=0xb49d420 {NSLocalizedRecoverySuggestion={"detail": "Authentication credentials were not provided."}, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest: 0xd834880> { URL: http://myHost:8000/api/test }, NSErrorFailingURLKey=http://myHost:8000/api/test, NSLocalizedDescription=Expected status code in (200), got 401, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xdc92ae0> { URL: http://myHost:8000/api/test/ } 
{ status code: 401, headers {
    Allow = "GET, POST, HEAD, OPTIONS";
    "Content-Type" = "application/json";
    Date = "Sat, 21 Dec 2013 17:17:22 GMT";
    Server = "WSGIServer/0.1 Python/2.7.3";
    Vary = Accept;
    "Www-Authenticate" = "Basic realm=\"api\"";
} }}

如果我不使用路径/ api / test但我使用root api(我不是特定的路径)我没有这个基本的Auth问题。 为什么RestKit仅使用基本身份验证的根API服务器? 我使用RestKit 0.20.3

0 个答案:

没有答案