title: iOS AFNetworking: Need help in uploading an image through multipart/form-data request
body: <p>I am trying to upload an image from my iOS native app to a Restful web service which is a multipart/form-data request. I am using AFNetworking library for this purpose. </p>
  
  <p>The HTML version of this part looks like below:</p>
  
  <pre><code>&lt;form action="rest/face/face8" method="post"
  enctype="multipart/form-data"&gt;
  
  UserId: &lt;input type="text" name="userId"
  value="cced82c0-513f-4125-95ac-ac37b3b58ee6"&gt;&lt;br&gt;
  
  Enrollment Id: &lt;input type="text" name="enrollmentId"
  value="c7d8466a-fe55-454e-aad7-964f13696899"&gt;&lt;br&gt;
  
  LicenseId: &lt;input type="text" name="licenseId"
  value="98a847ed-4148-4024-8265-6d3748468c3d"&gt;&lt;br&gt;
  
  ClientVersion: &lt;input type="text" name="clientVersion"
  value="sdk 3.3.4 - iPhone 5 (GSM) - iOS 7.0.2"&gt;&lt;br&gt;
  
  Select a file : &lt;input type="file" name="samples" size="45" /&gt;&lt;br&gt;
  
  &lt;input type="submit" value="Upload It" /&gt;
  </code></pre>
  
  <p>I am trying to replicate this in iOS app. My objective-C code looks like below:</p>
  
  <pre><code>UIImage *image= [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"IMG_0095" ofType:@"jpg"]];
  NSData *photoData= UIImageJPEGRepresentation(image, 0.8);
  
  AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://192.168.43.41:7001"]];
  [client setDefaultHeader:@"multipart/form-data; charset=utf-8; boundary=0xKhTmLbOuNdArY" value:@"Content-Type"];
  [client setDefaultHeader:@"close" value:@"Proxy-Connection"];
  [client setDefaultHeader:@"close" value:@"Connection"];
  //[client setDefaultHeader:[NSString stringWithFormat:@"%d", [photoData length]]  value:@"Content-Length"];
  [client setDefaultHeader:@"Mobile 2.1.0 (iPhone; iPhone OS 6.1.3; en_US)" value:@"User-Agent"];
  
  
  NSMutableURLRequest *request1 = [client multipartFormRequestWithMethod:@"POST" path:@"/Biometric/rest/face/face8" parameters:nil constructingBodyWithBlock: ^(id &lt;AFMultipartFormData&gt;formData) {
      [formData appendPartWithFormData:[[NSString stringWithFormat:@"cced82c0-513f-4125-95ac-ac37b3b58ee6"] dataUsingEncoding:NSUTF8StringEncoding] name:@"userId"];
      [formData appendPartWithFormData:[[NSString stringWithFormat:@"c7d8466a-fe55-454e-aad7-964f13696899"] dataUsingEncoding:NSUTF8StringEncoding] name:@"enrollmentId"];
      [formData appendPartWithFormData:[[NSString stringWithFormat:@"98a847ed-4148-4024-8265-6d3748468c3d"] dataUsingEncoding:NSUTF8StringEncoding] name:@"licenseId"];
      [formData appendPartWithFormData:[[NSString stringWithFormat:@"sdk 6.0.0 - iPhone 4s (GSM) - iOS 6.1.3"] dataUsingEncoding:NSUTF8StringEncoding] name:@"clientVersion"];
      [formData appendPartWithFileData:photoData name:@"samples" fileName:@"file" mimeType:@"application/octet-stream"];
  }];
  
  MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  hud.mode = MBProgressHUDModeDeterminate;
  hud.labelText = @"Connecting to Middleware...";
  
  [request1 setTimeoutInterval:180];
  
  AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request1];
  
  [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
      NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
      float progress = totalBytesWritten / (float)totalBytesExpectedToWrite;
      hud.progress= progress;
  }];
  
  [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
   {
       [MBProgressHUD hideHUDForView:self.view animated:YES];
       NSDictionary *jsons = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
       NSLog(@"response headers: %@", [[operation response] allHeaderFields]);
       NSLog(@"response: %@",jsons);
  
   }
                                   failure:^(AFHTTPRequestOperation *operation, NSError *error)
   {
       [MBProgressHUD hideHUDForView:self.view animated:YES];
       if([operation.response statusCode] == 403)
       {
           NSLog(@"Upload Failed");
           return;
       }
       NSLog(@"error: %@", [error debugDescription]);
  
   }];
  
  [operation start];
  </code></pre>
  
  <p>I am getting ** request timeout error ** after some time. It is not even reaching the server side platform. Please advise me what should I do here. Thanks ...</p>