I am trying to upload photo to my web-server via AFHTTPRequestOperation
. Here is my AFHTTPSessionManager
. All http-requests via this manager
works perfect.
- (AFHTTPSessionManager *)manager
{
if (_manager == nil) {
_manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:@"https://my.domain.com/api/v2"]];
[_manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"username" password:@"password"];
AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
policy.allowInvalidCertificates = YES;
_manager.securityPolicy = policy;
}
return _manager;
}
But when I am trying to upload photo with progress handling:
- (void)photoSend:(UIImage *)photo
toUsers:(NSArray *)users
completion:(CompletionHandler)completion
progress:(ProgressionHandler)progress
{
NSDictionary *params = @{
@"token":self.token,
@"to_usernames":[users componentsJoinedByString:@","],
};
NSString *urlString = [[NSURL URLWithString:@"photo/send" relativeToURL:self.manager.baseURL] absoluteString];
NSMutableURLRequest *request = [self.manager.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:urlString parameters:params constructingBodyWithBlock:^(id <AFMultipartFormData> formData)
{
[formData appendPartWithFileData:UIImageJPEGRepresentation(photo,0.8)
name:@"photo"
fileName:@"image.jpg"
mimeType:@"image/jpg"];
} error:nil];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
// Following 2 lines does not make sense. Http headers contains auth data without this code
//NSURLCredential *credential = [NSURLCredential credentialWithUser:@"login" password:@"password" persistence:NSURLCredentialPersistenceNone];
//[operation setCredential:credential];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
progress(totalBytesWritten*1.0/totalBytesExpectedToWrite);
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
completion(operation.response, responseObject, operation.error);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
completion(operation.response, nil, error);
}];
[self.manager.operationQueue addOperation:operation];
}
I get following error:
Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x994ff20 {NSErrorFailingURLKey=https://my.domain.com/v2/photo/send, NSErrorFailingURLStringKey=https://my.domain.com/v2/photo/send}