I am trying to use AFHTTPClient to make a rest api call. I am trying to make the api request by passing the Authentication key I got from registering to the api. When I run the app, I get 403 error. I am new to using AFNetworking and I would really appreciate any help on this.
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];
1 Answer 1
Thing with AFHTTPClient is that you set the base url in the init method.. then every requestWithMethod call.. you pass in a relative URL from the base... for example,
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"https://api.somewebsite.com/"]];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET"
path:@"/api/category/subcategory/fun/"
parameters:nil];
answered Mar 19, 2013 at 2:29
-
That is exactly is wrong with my code. That is really helpful. Thank you so much for saving me headache :)Harish– Harish2013年03月19日 10:26:21 +00:00Commented Mar 19, 2013 at 10:26
Explore related questions
See similar questions with these tags.
default