10

I'm migrating my app from ASIHTTPRequest to AFNetworking to talk to the backend API. Everything seems to work fine except by image uploading. I've used different examples over the internet, but running it on my app always causes a crash.

2013-02-22 17:02:28.680 MyApp[1477:907] *** Assertion failure in -[AFStreamingMultipartFormData appendPartWithHeaders:body:], AFNetworking/AFHTTPClient.m:885
2013-02-22 17:02:28.687 MyApp[1477:907] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: body'

The problem seems to be here:

NSMutableURLRequest *request = [[MyServiceAPIClient sharedClient] multipartFormRequestWithMethod:@"POST" path:@"/api/method" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:imageData name:@"face" fileName:@"face.jpg" mimeType:@"image/jpeg"];
}];

MyServiceAPIClient is a singleton class with as given in the AFNetworking iOS example app.

If I comment the appendPartWithFileData part everything runs fine, obviously it won't send my picture.

If I replace the multipart form request with a ordinary post request, it works. The only problem is appending my NSData to the form.

Any observations? Thanks.

2 Answers2

18

After looking into the AFNetworking/AFHTTPClient.m code, my guess is that your imageData is nil.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
2

Just to add, while Sulthan's answer is correct, you can generally get this error message by inserting any parameter with a nil value into formData.

Matthew Quiros
  • 13,385
  • 12
  • 87
  • 132
  • 1
    how can I solve this issue, please can you check my question as well http://stackoverflow.com/questions/22180367/afnetworking-2-2-0-upload-image-on-server-issues – Matrosov Oleksandr Mar 04 '14 at 18:57