Reputation: 1233
I want to POST form data using AFNetworking
. I am using this piece of code to achieve this:
// Create service request url
NSString *urlString = [NSString stringWithFormat:@"%@%@", kBaseURL, webServiceAPIName];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:@"myUser" forHTTPHeaderField:@"X-User-Agent"];
[manager.requestSerializer setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
// Set calling keys
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:@"5341" forKey:@"Id"];
[dict setObject:@"f1" forKey:@"refDataId"];
[dict setObject:@"f1" forKey:@"customRefDataId"];
[dict setObject:@"587" forKey:@"cost"];
[manager POST:urlString parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:UIImagePNGRepresentation(files[0]) name:@"ImageName" fileName:@"file1" mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"upload successful");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error image upload");
}];
After execution of this block after waiting some time it goes in Failure Section. Logging : "Error image upload"
without giving any error.
I tried my API in POSTMAN API CLIENT
and there it is working fine. I am able to send data and get response back.
And after running this block I am not able to run any other API call I have to stop my app and run again to run any other API call.
What is the issue with this code why I am not able to upload any form data and why it block my any other API calls?
Upvotes: 1
Views: 201
Reputation: 2185
Try below code:
-(void) uploadImage {
NSString *imagePath = [[NSUserDefaults standardUserDefaults] objectForKey:@"userimage"];
NSString * urlString = [stagingURL stringByReplacingOccurrencesOfString:@"user/" withString:@""];
NSString * uploadURL = @"Your URL where image to be uploaded";
NSLog(@"uploadImageURL: %@", uploadURL);
NSData *imageData = UIImageJPEGRepresentation([UIImage imageWithData:[NSData dataWithContentsOfFile:imagePath]], 0.5);
NSString *queryStringss = [NSString stringWithFormat:@"%@",uploadURL];
queryStringss = [queryStringss stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer=[AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
[manager POST:queryStringss parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"file" fileName:@"file" mimeType:@"image/jpeg"];
}
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];}
Upvotes: 0