0

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.

The HTML version of this part looks like below:

<form action="rest/face/face8" method="post"
enctype="multipart/form-data">

UserId: <input type="text" name="userId"
value="cced82c0-513f-4125-95ac-ac37b3b58ee6"><br>

Enrollment Id: <input type="text" name="enrollmentId"
value="c7d8466a-fe55-454e-aad7-964f13696899"><br>

LicenseId: <input type="text" name="licenseId"
value="98a847ed-4148-4024-8265-6d3748468c3d"><br>

ClientVersion: <input type="text" name="clientVersion"
value="sdk 3.3.4 - iPhone 5 (GSM) - iOS 7.0.2"><br>

Select a file : <input type="file" name="samples" size="45" /><br>

<input type="submit" value="Upload It" />

I am trying to replicate this in iOS app. My objective-C code looks like below:

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 <AFMultipartFormData>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];

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 ...

Rashmi Ranjan mallick
  • 6,390
  • 8
  • 42
  • 59
  • I cannot even hit that IP. 192.168.*.* is usually an internal router ip. You will need to enable port forwarding for port 7001 on your router to go to that IP address and inside your iOS code set your URL to your public IP. – Bot Nov 08 '13 at 16:46
  • @Bot: Thanks.. apart from that, do you see any coding mistake here in my code? – Rashmi Ranjan mallick Nov 08 '13 at 17:14
  • i don't but you can look at http://stackoverflow.com/questions/15410689/iphone-upload-multipart-file-using-afnetworking if you have any issues. – Bot Nov 08 '13 at 17:19

1 Answers1

0

Add / in the end of your BaseURL so it should look like below

AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://192.168.43.41:7001/"]];

and Remove / from your the url in your code below, and see if it works

NSMutableURLRequest *request1 = [client multipartFormRequestWithMethod:@"POST" path:@"Biometric/rest/face/face8" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData)
iphonic
  • 12,615
  • 7
  • 60
  • 107