0

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;
}
Vic85
  • 73
  • 9
  • 2
    Try replacing `NSData *requestData = [NSData dataWithBytes:[jsonRequestString UTF8String] length:[jsonRequestString length]];` with `NSData *requestData = jsonRequestData`. – HangarRash Aug 16 '23 at 16:50
  • Have you used the debugger to see what is actually happening at runtime? – HangarRash Aug 16 '23 at 16:52
  • What's happening? What are the logs saying? What's the cURL equivalent in POSTMAN, or the Objective-C generated code? What's calling that method? Is the object deallocated too soon? – Larme Aug 16 '23 at 18:08
  • @HangarRash Thank you very much:) Don't know I was messing around with NSData!! – Vic85 Aug 17 '23 at 01:52

0 Answers0