2013-03-19 48 views
0

我正在嘗試使用AFHTTPClient進行其他API調用。我試圖通過將註冊的認證密鑰傳遞給api來完成api請求。當我運行該應用程序時,出現403錯誤。我是使用AFNetworking的新手,我非常感謝這方面的幫助。使用AFHttpClient請求使用驗證密鑰的API

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL  URLWithString:@"http://somewebsite.com/"]]; 

[httpClient setDefaultHeader:@"Authorization: Client-ID" value:@"xxxxxxxx"]; 

NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET" 
                 path:@"https://api.somewebsite.com/api/category/subcategory/fun/" 
                parameters:nil]; 

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]]; 

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
    // Print the response body in text 
    NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]); 
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Error: %@", error); 
}]; 
[operation start]; 

回答

2

事情與AFHTTPClient是您設置的基礎URL在init()方法..然後每requestWithMethod通話..你在一個相對URL傳遞從基...例如,

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL  URLWithString:@"https://api.somewebsite.com/"]]; 

NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET" 
                 path:@"/api/category/subcategory/fun/" 
                parameters:nil]; 
+0

這是我的代碼完全錯誤。這真的很有幫助。非常感謝你挽救我的頭痛:) – Harish 2013-03-19 10:26:21