AFNetworking AFHTTPRequestOperation block is never called
I am using AFNetworking to submit a multipart form to a web server and I am having problems with mine AFHTTPRequestOperation
. Its successful and failover blocks are never called after I run it.
Here is my code (its summary)
NSMutableURLRequest *request = [[ServerAPI sharedClient] multipartFormRequestWithMethod:@"POST" path:postUrl parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
[formData appendPartWithFileData:picture.picture_data name:@"InputFile" fileName:picture.name mimeType:@"image/jpg"];
}];
AFHTTPRequestOperation *operation = [[ServerAPI sharedClient] HTTPRequestOperationWithRequest: request success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success");
} failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure");
}];
[operation setUploadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
NSLog(@"%f", (totalBytesRead / (float) totalBytesExpectedToRead));
}];
[[ServerAPI sharedClient] enqueueHTTPRequestOperation:operation];
I see the progress logs, but the success and failure blocks are never called.
picture.picture_data
is NSData
, initialized a UIImageJPEGRepresentation(image, 0.7)
ServerAPI is a subclass AFHTTPClient
, and sharedCliend
is a one-point method.
What are the reasons for AFNetworking not triggering my blocks, even with the correct error message?
Thanks everyone!
Edit
I get a request with the same url before this and it works as usual. The url I'm using:part/_layouts/UploadEx.aspx?List=%7BD432BF97-7175-40C1-8E0D-27D8661CBC90%7D&RootFolder=%2Fpwa%2Fpart%2FLibrary&Source=http%3A%2F%2Fwww%2Emysite%2Ecom%2Fpwa%2Fpart%2FLibrary%2FForms%2FAllItems%2Easpx&IsDlg=1
source to share
Check in your code postUrl
. BaseURL+postURL
must be valid. Try uploading the image with a regular web browser using the url BaseURL+postURL
.
Edit
method HTTPRequestOperationWithRequest:success:failure:
doesn't work for file uploads, but does work for json / html fetching. Try to use
AFHTTPRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
NSLog(@"%f", (totalBytesRead / (float) totalBytesExpectedToRead));
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure");
}];
[[ServerAPI sharedClient] enqueueHTTPRequestOperation:operation];
source to share