Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking - Requests with ETag

I am trying to do a request on a server, which responds with an Etag for cache purposes. I have written the following code for it, but the response for these calls is random most of the time i.e. sometimes response status code is 200 and some times 304(expected). Am I doing something wrong in my code or is there something specific with AFNetworking that I should keep in mind.!

  NSURL *url = [NSURL URLWithString:@"http://ia.media-imdb.com/images/M/MV5BNzI0NTY5NzQwMV5BMl5BanBnXkFtZTcwOTQyNTA5OA@@._V1._SY90_.jpg"];
  NSMutableURLRequest *aRequest = [NSMutableURLRequest requestWithURL:url];
  [aRequest setValue:@"\"61-smtLpBSL_SY90_#1\"" forHTTPHeaderField:@"If-None-Match"];
  NSLog(@"headers: %@", aRequest.allHTTPHeaderFields);
  AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:aRequest imageProcessingBlock:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
    NSLog(@"%@ %d", response.allHeaderFields, response.statusCode);
  } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
    NSLog(@"Request failed with error: %@", error);    
  }];
  [operation start];
like image 740
Ayush Goel Avatar asked Oct 06 '22 11:10

Ayush Goel


1 Answers

I had the same problem, I managed to fix this by changing my request to

  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]
                                                               cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                                           timeoutInterval:60];
like image 106
Mike Bryant Avatar answered Oct 10 '22 03:10

Mike Bryant