Reputation: 2738
I am trying to get a simple GET request to work for my api. But it doesnt seem to work, and I am not sure why.
My code is as follows
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *urlAsString = @"https://www.test.com/api/v1/user/logout/";
NSURL *url = [NSURL URLWithString:urlAsString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url]; [urlRequest setTimeoutInterval:30.0f];
[urlRequest setHTTPMethod:@"GET"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response,NSData *data, NSError *error) {
if ([data length] >0 && error == nil){
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"HTML = %@", html); }
else if ([data length] == 0 && error == nil){
NSLog(@"Nothing was downloaded."); }
else if (error != nil){
NSLog(@"Error happened = %@", error); }
}];
}
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
My API spits out json. So I should be getting a json object back that says
success: False
reason: 'Already Logged out'
But instead it gives me the following error
2013-03-07 16:24:44.038 test[8957:1d03] Error happened = Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x715db20 {NSErrorFailingURLKey=https://www.test.com/api/v1/user/logout/, NSErrorFailingURLStringKey=https://www.test.com/api/v1/user/logout/, NSUnderlyingError=0x7181cb0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1012.)"}
after some reseach, I found another way of sending get requests, and this method seems to work fine
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSError *error = nil;
NSURL *url = [NSURL URLWithString:@"https://www.test.com/api/v1/user/logout/"];
NSString *json = [NSString stringWithContentsOfURL:url
encoding:NSASCIIStringEncoding
error:&error];
if(!error) {
NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData
options:kNilOptions
error:&error];
BOOL success = [[jsonDict objectForKey:@"success"] boolValue];
if (!success) {
NSString *reason = [jsonDict objectForKey:@"reason"];
NSLog(@"Cannot log out, as user %@", reason);
}
//NSLog(@"JSON: %@", jsonDict);
}else{
NSLog(@"\nJSON: %@ \n Error: %@", json, error);
}
});
Upvotes: 2
Views: 466
Reputation: 2738
The solution to the problem was in the API itself. The API was supposed to send HTTPUnauthorized signal if the user is already logged out. And since that was the case, iOS was showing all these exceptions. when I dropped that and just sent back a simple json response, everything got fixed.
Upvotes: 1