0

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];
asked Mar 19, 2013 at 1:13

1 Answer 1

2

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
1
  • That is exactly is wrong with my code. That is really helpful. Thank you so much for saving me headache :) Commented Mar 19, 2013 at 10:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.