0

The multipart upload of an image worked fine with AFNetwrking 1.0 and I need to upgrade to 3.0. However, the request parameters and the body disappear. It seems to be in AFURLRequestSerialization.m the statement:

[self.request setHTTPBodyStream:self.bodyStream]

even though the parts and body are present in self.bodyStream. There doesn't seem be an error returned from setHTTPBodyStream.

This is the code:

AFHTTPRequestSerializer *requestSerializer = [AFHTTPRequestSerializer serializer];

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

manager.securityPolicy.allowInvalidCertificates = YES; // not recommended for production

manager.securityPolicy.validatesDomainName = NO;

[requestSerializer setValue:@"multipart/form-data" forHTTPHeaderField:@"content-type"];

NSMutableURLRequest *request = [requestSerializer multipartFormRequestWithMethod:@"POST" URLString: ServerPath
                    parameters:sendDictionary constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

[formData appendPartWithFileData: imageData name:@"file" fileName:@"temp.jpeg" mimeType:@"image/jpeg"];
                         } error:nil];

[request setTimeoutInterval:20000];


NSURLSessionUploadTask *uploadTask;

NSLog(@"Request body %@", [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]); NSLog(@"Request HTTP Headers: %@", request.allHTTPHeaderFields); NSLog(@"%@", [request HTTPBody]);

uploadTask = [manager
bugman
  • 44
  • 3

1 Answers1

0

AFNetwrking 3.0 clearly must be used from Swift code because it doesn't support manual memory management anymore. Unfortunately you have to rewrite the Objective C code.

Marie
  • 105
  • 6
  • Well, I'm astounded because there are lots of examples around in objective c claiming to work – bugman Apr 27 '18 at 16:54
  • things break down fast if it's Apple, sorry – Marie Apr 28 '18 at 05:29
  • Well, I reverted to my old AFNetorking 1.0 version and the same behaviour. Tried another technique from this forum using: `appendPartWithFileURL and appendPartWithFormData` and it didn't work either. Went back to the original version and now it works! Talk about confusing. I will take the opportunity to learn swift. thank you for your reply – bugman Apr 28 '18 at 07:55