1

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
NSUser
  • 1,233
  • 9
  • 15
  • 1
    Are you using the latest version of the framework? Is `NSError *error` definitely nil in the failure block? In your `appendPartWithFileData:` call, for testing purposes I'd take that line of code out and check that the `NSData` object is also not nil. – Luke Oct 07 '15 at 08:32
  • @Luke This is the error I am getting in NSError `*error` – NSUser Oct 07 '15 at 09:27
  • `Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x7fde1ae09790 {NSUnderlyingError=0x7fde1af158d0 "The request timed out.", NSErrorFailingURLStringKey=apikey, NSErrorFailingURLKey=apikey, NSLocalizedDescription=The request timed out.}` – NSUser Oct 07 '15 at 09:27
  • Is this in the simulator? If so, possible duplicate issue here: http://stackoverflow.com/questions/26972822/error-error-domain-nsurlerrordomain-code-1001-the-request-timed-out Also, check you're _not_ currently running the Network Link Conditioner app on your Mac - if you don't know what this is, then don't worry ;) – Luke Oct 07 '15 at 10:05

1 Answers1

0

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);
 }];}
Macrosoft-Dev
  • 2,195
  • 1
  • 12
  • 15