1

Hi I am developing small IOS application in which I want to send json array as post data. I tried it in following manner :

NSString *sampleParam = @"[{\"user:\"\"me Nilesh \"}]";
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];

NSString *urlString = @"ABC.com";

NSURL * url = [NSURL URLWithString:urlString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url
                                                          cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                      timeoutInterval:60.0];

[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[sampleParam dataUsingEncoding:NSUTF8StringEncoding]];

NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
                                                   completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
                                  {
                                      dispatch_async(dispatch_get_main_queue(),^
                                     {
                                       // handle result here ... 
                                     });
                                 }];
                                [dataTask resume];

I got log records like this

 -[GCKDeviceScanner startNetServiceScan]  startNetServiceScan
 -[GCKDeviceScanner stopNetServiceScan]  stopNetServiceScan

Status code coming as zero and error is request timeout after something.

So here is my scenario I tried to send json array data to my server. Once I start my API call after some time it gives me status code as zero and request timeout error. But main thing is that it is updating data on my server. then why it is giving error with status code zero. Is there anything which I am doing wrong or something which I am missing. It keep giving that error. Is it because something NSURLSessionConfiguration.

nilkash
  • 7,408
  • 32
  • 99
  • 176

1 Answers1

0

Maybe your server need specified content type in your POST request:

[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
Vitalii Gozhenko
  • 9,220
  • 2
  • 48
  • 66