Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Multipart Zip file sending successfully in Post man but not working in Objective C

Multipart Zip file sending successfully in Post man but not working in Objective C

post man we are getting response But objective c we are getting response nil

#import <Foundation/Foundation.h>

Below code copied form post man

NSDictionary *headers = @{ @"content-type": @"multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
                           @"authorization": @"5715C2E6-8DF7-4EF1-9433-51888359F96E",
                           @"cache-control": @"no-cache",
                           @"postman-token": @"aa51ccb9-1a2e-1003-7aa4-3a665770c2aa" };
NSArray *parameters = @[ @{ @"name": @"AttributeId", @"value": @"0" },
                         @{ @"name": @"Comments", @"value": @"Test comments" },
                         @{ @"name": @"FarmerId", @"value": @"0" },
                         @{ @"name": @"Remarks", @"value": @"Remarks" },
                         @{ @"name": @"Subject", @"value": @"Subject" },
                         @{ @"name": @"activityId", @"value": @"0" },
                         @{ @"name": @"assignmentId", @"value": @"0" },
                         @{ @"name": @"createdBy", @"value": @"604" },
                         @{ @"name": @"ctreatedDate", @"value": @"05-11-2018" },
                         @{ @"name": @"eventTypeId", @"value": @"211" },
                         @{ @"name": @"imageId", @"value": @"1" },
                         @{ @"name": @"pdaID", @"value": @"5214" },
                         @{ @"name": @"picturePath", @"fileName": @"604.zip" },
                         @{ @"name": @"referenceId", @"value": @"1239488" },
                         @{ @"name": @"referenceType", @"value": @"0" },
                         @{ @"name": @"updatedBy", @"value": @"604" },
                         @{ @"name": @"updatedDate", @"value": @"05-11-2018" } ];
NSString *boundary = @"----WebKitFormBoundary7MA4YWxkTrZu0gW";

NSError *error;
NSMutableString *body = [NSMutableString string];
for (NSDictionary *param in parameters) {
    [body appendFormat:@"--%@\r\n", boundary];
    if (param[@"fileName"]) {
        [body appendFormat:@"Content-Disposition:form-data; name=\"%@\"; filename=\"%@\"\r\n", param[@"name"], param[@"fileName"]];
        [body appendFormat:@"Content-Type: %@\r\n\r\n", param[@"contentType"]];
        [body appendFormat:@"%@", [NSString stringWithContentsOfFile:param[@"fileName"] encoding:NSUTF8StringEncoding error:&error]];
        if (error) {
            NSLog(@"%@", error);
        }
    } else {
        [body appendFormat:@"Content-Disposition:form-data; name=\"%@\"\r\n\r\n", param[@"name"]];
        [body appendFormat:@"%@", param[@"value"]];
    }
}
[body appendFormat:@"\r\n--%@--\r\n", boundary];
NSData *postData = [body dataUsingEncoding:NSUTF8StringEncoding];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://trens.com/Demo/FileAPI/api/PostReports"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];

Below code in objective C
#import <Foundation/Foundation.h>


NSString *token = [[FCCommon sharedInstance] getUserDefaultsObjectWithKey:K_X_AUTH_TOKEN];

    NSDictionary *headers = @{ @"content-type": @"multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
                               @"authorization": token};
    NSArray *parameters = @[ @{ @"name": @"referenceId", @"value": @"1239488" },
                             @{ @"name": @"referenceType", @"value": @"qeqeqweqwdsf" },
                             @{ @"name": @"updatedBy", @"value": @"604" },
                             @{ @"name": @"updatedDate", @"value": @"05-11-2018" },
                             @{ @"name": @"AttributeId", @"value": @"0" },
                             @{ @"name": @"Comments", @"value": @"test comments" },
                             @{ @"name": @"Remarks", @"value": @"test remarks" },
                             @{ @"name": @"Subject", @"value": @"test subject" },
                             @{ @"name": @"activityId", @"value": @"0" },
                             @{ @"name": @"assignmentId", @"value": @"0" },
                             @{ @"name": @"createdBy", @"value": @"604" },
                             @{ @"name": @"ctreatedDate", @"value": @"05-11-2018" },
                             @{ @"name": @"eventTypeId", @"value": @"211" },
                             @{ @"name": @"imageId", @"value": @"2" },
                             @{ @"name": @"pdaID", @"value": @"5214" },
                             @{ @"name": @"picturePath", @"fileName": @"604.zip" },
                             @{ @"name": @"FarmerId", @"value": @"0" } ];
    NSString *boundary = @"WebKitFormBoundary7MA4YWxkTrZu0gW";

    NSError *error;
    NSMutableString *body = [NSMutableString string];
    for (NSDictionary *param in parameters) {
        [body appendFormat:@"\r\n--%@\r\n", boundary];
        if (param[@"fileName"]) {
            [body appendFormat:@"Content-Disposition:form-data; name=\"%@\"; filename=\"%@\"\r\n", param[@"name"], param[@"fileName"]];
            [body appendFormat:@"Content-Type: application/zip\r\n"];
            [body appendFormat:@"%@\r\n",[NSString stringWithContentsOfFile:zipFilePath encoding:NSUTF8StringEncoding error:&error]];


            if (error) {
                NSLog(@"%@", error);
            }
        } else {
            [body appendFormat:@"Content-Disposition:form-data; name=\"%@\"\r\n", param[@"name"]];

            [body appendFormat:@"%@\r\n", param[@"value"]];
        }
    }
    [body appendFormat:@"\r\n--%@--\r\n", boundary];
    NSData *postData = [body dataUsingEncoding:NSUTF8StringEncoding];

    NSString *rawString = [[NSString alloc] initWithData:postData encoding:NSUTF8StringEncoding];


    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://trens.com/Demo/FileAPI/api/PostReports`enter code here`"]
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:60.0];
    [request setHTTPMethod:@"POST"];
    [request setAllHTTPHeaderFields:headers];
    [request setHTTPBody:postData];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                                completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                    if (error) {
                                                        NSLog(@"%@", error);
                                                    } else {
                                                        NSDictionary* responseDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
                                                        NSLog(@"responseDict %@",responseDict);
                                                        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                        NSLog(@"httpResponse %@", httpResponse);
                                                    }
                                                }];
    [dataTask resume];
}

0/P Response is nil

enter image description here

Comments