iosobjective-cafnetworkingafnetworking-2

AFNetworking fails to create a request on a GET request


I'm trying to make a get request to a restful service. This request has worked for the last 3 weeks when I was using a request without a request manager. As soon as I added a request manager because I wanted to send a dictionary of parameters, the code keeps crashing with the error:

'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: urlRequest'

My code is as follows. The commented out code still works if I uncomment it and replace the new code with it. Why am I able to make a request from that URL but not with AFNetworking?

NSString *URLForSend = [NSString stringWithFormat:@"%@%@/", ([_remoteDownloadURLString stringByAppendingString:_getFilesJsonURLString]),_userName ];
    NSURL *URL = [NSURL URLWithString:URLForSend];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    self.downloadJsonRequest = [[AFHTTPRequestOperation alloc] initWithRequest:request];//this works to create a a afhttprequestoperation, with the same url
    _downloadJsonRequest.responseSerializer =
    [AFJSONResponseSerializer serializerWithReadingOptions: NSJSONReadingMutableContainers];
    __unsafe_unretained typeof(self) weakSelf = self;

    NSMutableDictionary *mutDictForSend = [[NSMutableDictionary alloc] initWithDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:@"tokenInfo"]];
    [mutDictForSend removeObjectForKey:@"success"];
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
    [manager GET:URLForSend parameters:mutDictForSend success:^(AFHTTPRequestOperation *operation, id responseObject)
     {

         NSLog(@"%@", responseObject);
         weakSelf.jsonArrayForCaching = responseObject;
         [[NSNotificationCenter defaultCenter] postNotificationName:kBIDContentEndingSync object:weakSelf];
         [weakSelf gotNewJson:responseObject];
     }
         failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         NSLog(@"Request Failure Because %@",[error userInfo]);
     }];
    /*
    [_downloadJsonRequest setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
     {

         NSLog(@"%@", responseObject);
         weakSelf.jsonArrayForCaching = responseObject;
         [[NSNotificationCenter defaultCenter] postNotificationName:kBIDContentEndingSync object:weakSelf];
         [weakSelf gotNewJson:responseObject];
     } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         NSLog(@"Request Failure Because %@",[error userInfo]);
     }];
    [[NSNotificationCenter defaultCenter] postNotificationName:kBIDContentStartingSync object:self];
    [_downloadJsonRequest start];
     */

Solution

  • I'd suggest replacing the line:

    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
    

    With

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    

    The latter calls initWithBaseURL, which does some configuration of the AFHTTPRequestOperationManager, that just calling [[AFHTTPRequestOperationManager alloc] init] does not appear to do.

    If you're still having issues even doing that, I'd suggest adding Exception Breakpoint to identify precisely where this exception is taking place.