Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assertion failure appending to multipart form

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.

like image 624
Marco Torres Morgado Avatar asked Feb 22 '13 20:02

Marco Torres Morgado


2 Answers

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

like image 120
Sulthan Avatar answered Oct 14 '22 02:10

Sulthan


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.

like image 37
MLQ Avatar answered Oct 14 '22 02:10

MLQ