We have some code posting an image and some other data as part of a Multipart in a POST request. In Android is working just fine, but I can't seem to make it work on iOS, where I keep getting a 500 Internal Server Error. The working Android code looks like
String uploadURL = "http://someServer.com/upload";
String imageToUploadPath = "imgFilePath";// path de la imagen a subir
String userId = "123";
String token = "abcd";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(uploadURL);
File f = new File(imageToUploadPath);
FileBody fileBody = new FileBody(f);
MultipartEntity reqEntity = new MultipartEntity();
Charset chars = Charset.forName("ISO-8859-1");
reqEntity.addPart("id", new StringBody(userId, chars));
reqEntity.addPart("token", new StringBody(token, chars));
reqEntity.addPart("image", fileBody);
httppost.setEntity(reqEntity);
HttpResponse response = httpClient.execute(httppost);
My iOS attempt using AFNetworking is as follows
uploadURLStr = @"http://someServer.com/upload";
NSString *token = @"abcd";
NSString *userID = @"123";
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSMutableSet *supportedContentTypes = [manager.responseSerializer.acceptableContentTypes mutableCopy];
supportedContentTypes addObject:@"text/html"];
manager.responseSerializer.acceptableContentTypes = supportedContentTypes;
NSURLSessionTask *task = [manager POST:uploadURLStr
parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSDictionary *tokenHeaders = @{@"Content-Disposition": @"form-data",
@"name": @"token",
@"Content-Type": @"text/plain",
@"charset": @"ISO-8859-1",
@"Content-Transfer-Encoding": @"8bit"
};
[formData appendPartWithHeaders:tokenHeaders body:[token dataUsingEncoding:NSISOLatin1StringEncoding]];
NSMutableDictionary *userIDHeaders = [tokenHeaders mutableCopy];
[userIDHeaders setObject:@"id" forKey:@"name"];
[formData appendPartWithHeaders:[userIDHeaders copy] body:[userID dataUsingEncoding:NSISOLatin1StringEncoding]];
NSDictionary *imgHeaders = @{@"Content-Disposition": @"form-data",
@"name": @"image",
@"filename": fileName,
@"Content-Type": @"application/octet-stream",
@"Content-Transfer-Encoding": @"binary"
};
[formData appendPartWithHeaders:imgHeaders
body:[imgData base64EncodedDataWithOptions:0]];
}
progress:^(NSProgress *uploadProgress) {
NSLog(@"progress: %.2f", uploadProgress.fractionCompleted);
}
success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"responseObject = %@", responseObject);
} failure:^(NSURLSessionTask *task, NSError *error) {
[self showUploadError];
NSLog(@"error = %@", error);
}];
Headers for each part we managed to log it from the Android app so we replicated it -for instance, I was sending the image with a Content-Type of image/jpeg and Android sends it as application/octet-stream- I assume that the image data is not being encoded as in Android. I have tried with base64 encoding as it's in the code now -tried different options other than 0-, just leaving the NSData returned by UIImageJPEG representation but I can't hit the nail. Any help is appreciated.