Objective C POST call not executing NSURLSession

34 Views Asked by At

I would like to call a POST url and pass JSON data. For some reason my NSURLSessionDataTask is not firing at all I believe. I do not see any issues with my JSON data and api url is working fine (tested with postman). Could someone please let me know, what am I doing wrong here? Please find the code snippet bellow. TIA

-(bool)CallPayPass:(PaymentConfig *)aConfig
{
    
    NSData *jsonRequestData = [self GETJSONData:aConfig];
    
    if (!jsonRequestData)
    {
        NSLog(@"Failed to create JSON data");//Handle Error Here. Later
    }
    else
    {
        NSString *jsonRequestString = [[NSString alloc] initWithData:jsonRequestData encoding:NSUTF8StringEncoding];
        
        //Step 1: Initialize transaction and get transactionnumber
        NSURL *url = [NSURL URLWithString:@"https://api.sandbox.paypass.com/api/transactions/init"];
        
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
        NSData *requestData = [NSData dataWithBytes:[jsonRequestString UTF8String] length:[jsonRequestString length]];
        [request setHTTPMethod:@"POST"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
        [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[requestData length]] forHTTPHeaderField:@"Content-Length"];
        [request setHTTPBody: requestData];
        
        NSURLSession *session = [NSURLSession sharedSession];
        NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                                    completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
        {
            // do something with the data
            NSData *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
            
            if (!json)
            {
                NSLog(@"Failed to create JSON response data: %@", [error localizedDescription]);
            }
            else
            {
                NSLog(@"%@", json);
                NSString *jsonResponseString = [[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding];
                NSLog(@"%@", jsonResponseString);
            }
            
            
            
        }];
        [dataTask resume];
    
        
    }
    return YES;
}
0

There are 0 best solutions below