AFNetworking with AFJSONRequestOperation and filedata never completes
I am using AFNetworking to send json data to my webservice and receive json response. However, now I also want to add multipart formdata to these POSTs. If I do this (even without the parameters I added first) the completion block never fires. The execution block STARTS and I can see that the file is loading correctly.
Does anyone have any experience posting images like this with AFNetworking? I am using the latest source / version of AFNetworking.
This is my initial code to put a dictionary with json in it. This works great.
NSMutableDictionary *postdata = [[NSMutableDictionary alloc] init];
[postdata setObject:[postdictionary JSONString] forKey:@"request"];
NSMutableURLRequest *jsonRequest = [httpClient requestWithMethod:@"POST"
path:path
parameters:postdata];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:jsonRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {// Success} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {// Failure }];
[operation start];
This is the version of the code to send the image (doesn't work)
NSMutableURLRequest *jsonRequest jsonRequest = [httpClient multipartFormRequestWithMethod:@"POST" path:path parameters:postdata constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"photo" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
[formData throttleBandwidthWithPacketSize:5000 delay:0.1];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:jsonRequest];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Upload succes");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Upload failed");
}];
[operation start];
source to share
I am using this code for several applications and havent had any problems with it (please note that I am using AFJSONRequestOperation and AFRestClient)
ask request
// I have a shared singleton in a separate file APIClient.h
AFRESTClient *httpClient = [APIClient sharedClient];
NSURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:method parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData> formData)
{[formData appendPartWithFileData:imageData
name:@"image"
fileName:@"image.png"
mimeType:@"image/png"];
}];
then the operation
AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:request];
my execution block
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
CGFloat progress = ((CGFloat)totalBytesWritten) / totalBytesExpectedToWrite * 100;
[SVProgressHUD showProgress:50 status:[NSString stringWithFormat:@"%0.1f %%", progress]];
}];
success block (my API returns stat -ok or fail then data (same as flickr API)
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id response) {
NSLog(@"response string: %@", operation.responseString);
NSString *resultStat = [response objectForKey:@"stat"];
if ([resultStat isEqualToString:@"ok"]) {
//success
// do something with your data that is returned from the API
} else {
//error
NSLog(@"Data Error: %@", response);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", [error localizedDescription]);
}];
[httpClient enqueueHTTPRequestOperation:operation];
source to share