我正在尝试使用AFHTTPClient进行休息api调用。我试图通过传递我从注册到api获得的身份验证密钥来发出api请求。当我运行应用程序时,我收到403错误。我是新手使用AFNetworking,我真的很感激任何帮助。
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];
答案 0 :(得分:2)
AFHTTPClient的用法是你在init方法中设置基本URL ..然后每个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];