我正在使用RESTKit實現GET請求,並與該請求我想有一個自定義http標頭。爲了讓GET請求獲得所需的數據,我需要發送一個令牌(作爲變量),這是我在頭文件中的。但是,當我在控制檯中查看響應時,它給了我一個401狀態代碼,這意味着網站沒有獲得自定義http頭。我究竟做了什麼錯誤導致自定義標題不起作用。如何實現自定義HTTP標頭RESTKit
這裏是我的代碼:
NSIndexSet *statusCodeSet = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
NSString *urlString = [NSString stringWithFormat:@"https://foo.com/foo/:foo_number/providers/find?name=%@&location=%@", nameIDTextField.text, locationTextField.text];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//Here is my custom header code
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:url];
[objectManager.HTTPClient setDefaultHeader:@"Auth-Token" value:[[self userAuthTokenMethod] userAuthToken]];
//End of custom header code
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"Stuff Here ==> %@", connection);
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[MappingProvider tokenMapping]
pathPattern:@"/v2/styles"
keyPath:@"data"
statusCodes:statusCodeSet];
NSURLRequest *doctorRequest = [NSURLRequest requestWithURL:url];
RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:doctorRequest
responseDescriptors:@[responseDescriptor]];
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{
NSLog(@"Mapping Results ==> %@", mappingResult.array);
}
failure:^(RKObjectRequestOperation *operation, NSError *error)
{
NSLog(@"ERROR: %@", error);
NSLog(@"Response: %@", operation.HTTPRequestOperation.responseString);
}];
[operation start];
編輯:
以下是錯誤日誌的一部分:
2013-08-01 22:42:58.547 Empyrean[78461:5803] 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-299), got 401"
如果您發現任何其他問題的代碼,請隨時自由指出。
要與restkit頭這就像你沒有設置一對鍵/值,用'setDefaultHeader:值:您的ObjectManager的客戶端的'。也許問題在別的地方。請將請求/響應數據發佈到restkit。在日誌中,您可以看到標題是否在請求中發送。 –
打開RK記錄,通過設定'RKLogConfigureByName(「RestKit /網絡」,RKLogLevelTrace);'和檢查,如果你的頭確實缺少 –
我加入了日誌的一部分 – comrod