objective-cfile-uploadmultipartform-dataafnetworking-2advanced-rest-client

Convert Chrome Advanced Rest Client request to AFNetworking 2 request


Need help here, I'm trying to upload a file to a secured API, I was able to upload successfully using the Advanced REST Client chrome extension but when I tried converting it to an afnetworking post request, it always return a request timed out error, I've been doing this for 3 days but I always receive request timed out error, tried extending the timeout interval but to no avail.

My Code:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

[[CookieHandler sharedManager] cookieEaterUsingCookiesInURL:OC_BASE_URL];
NSString *csrftoken = [[CookieHandler sharedManager] getCSRFToken];

[manager.requestSerializer setValue:csrftoken forHTTPHeaderField:COOKIES_CSRFTOKEN_KEY];
[manager.requestSerializer setTimeoutInterval:120];

AFHTTPRequestOperation *operation = [manager POST:[self composeFullUrlWithRestApi:uploadUrl] parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:fileData name:@"fileUpload" fileName:documentName mimeType:mimeType];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

[operation setUploadProgressBlock:^(NSUInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
    NSLog(@"Wrote %ld/%ld", (long)totalBytesWritten, (long)totalBytesExpectedToWrite);
}];

Printing the operation variable yields:

<AFHTTPRequestOperation: 0x15731040, state: isExecuting, cancelled: NO request: <NSMutableURLRequest: 0x15523180> { URL: https://preprod.myUrl.com/uploadApi, headers: {
    "Accept-Language" = "en;q=1";
    CSRFToken = "cfbaf91f-6f2b-4ed1-a094-06c8eec6f757";
    "Content-Length" = 32943;
    "Content-Type" = "multipart/form-data; boundary=Boundary+B7082B5EB6A9063A";
    "User-Agent" = "ObjectiveConnect/5 (iPhone; iOS 8.0.2; Scale/2.00)";
} }, response: (null)>

Printing the error yields:

Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." 
UserInfo=0x1561fec0 {NSErrorFailingURLStringKey=https://preprod.myUrl.com/uploadApi, 
NSErrorFailingURLKey=https://preprod.myUrl.com/uploadApi, 
NSLocalizedDescription=The request timed out., NSUnderlyingError=0x1567c400 "The request timed out."}

Solution

  • Based on this: Related stackoverflow post

    I should mention that this might be a server's lacking to support AFNetworking request or the request can't work with all servers.

    So based on that notion, I went on doing the manual creation of request, which fortunately was also provided with the answer on that post so this is my version:

    typedef void (^uploadFileCompletionBlock)(NSURLResponse *response, id responseObject, NSError *error);
    - (NSMutableURLRequest *)buildRequestWithUrl:(NSString *)url
                                    withFileData:(NSData *)paramData
                                        fileName:(NSString *)name
                                 fileContentType:(NSString *)fileContentType
    {
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:[NSURL URLWithString:[self composeFullUrlWithRestApi:url]]];
        [request setHTTPMethod:@"POST"];
    
        [[CookieHandler sharedManager] cookieEaterUsingCookiesInURL:OC_BASE_URL];
    
        NSString *csrftoken = [[CookieHandler sharedManager] getCSRFToken];
    
        [request setValue:csrftoken forHTTPHeaderField:COOKIES_CSRFTOKEN_KEY];
    
        NSString *boundary = @"0xKhTmLbOuNdArY";
        NSString *endBoundary = [NSString stringWithFormat:@"\r\n--%@--", boundary];
    
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
        [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    
        [request setValue:@"*/*" forHTTPHeaderField:@"Accept"];
        NSMutableData *tempPostData = [NSMutableData data];
        [tempPostData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    
        // Sample file to send as data
        [tempPostData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"\r\n", name] dataUsingEncoding:NSUTF8StringEncoding]];
        [tempPostData appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", fileContentType] dataUsingEncoding:NSUTF8StringEncoding]];
        [tempPostData appendData:paramData];
        [tempPostData appendData:[endBoundary dataUsingEncoding:NSUTF8StringEncoding]];
        [request setHTTPBody:tempPostData];
    
        return request;
    }
    
    - (void) uploadFileWithFileData:(NSData *)fileData
                    andDocumentName:(NSString *)documentName
                    fileContentType:(NSString *)mimeType
                          uploadUrl:(NSString *)uploadUrl
                 andCompletionBlock:(uploadFileCompletionBlock)completionBlock
    {
        NSMutableURLRequest* request = [self buildRequestWithUrl:uploadUrl withFileData:fileData fileName:documentName fileContentType:mimeType];
    
        [NSURLConnection sendAsynchronousRequest:request queue:[[AFHTTPRequestOperationManager manager] operationQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            completionBlock(response, data, connectionError);
        }];
    }
    

    TL;DR

    If your AFNetworking request is failing you, do the manual request construction way, you need to learn a little about request headers to understand this.