Reputation: 1519
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];
Upvotes: 0
Views: 202
Reputation: 5409
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];
Upvotes: 2