4

I am making a REST web Service call using NSURLSession.I have set the content-Type for the webservice in NSURLSessionConfiguration.HTTPAdditionalHeaders. The below code was working absolutely fine in iOS 7.0 to iOS 8.1.3. But in iOS 8.3 the web service response data is returned as zero bytes when the content-Type is set in HTTPAdditionalHeaders.

Method 1:

NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] init];
NSData *postData = [dataToBeSent dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
[urlRequest setHTTPBody:postData];
[urlRequest setHTTPShouldHandleCookies:YES];
[urlRequest setURL:urlOfService];
[urlRequest setHTTPMethod:@"POST"];

NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.HTTPAdditionalHeaders = @{@"Content-Type"       : @"application/json"};
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
}];

If I modify the above code to set the content-Type in NSMutableURLRequest as below, I receive data from webservice in iOS8.3. I am not sure what is causing this issue. Also I do not see anyone reporting this issue. I just want to know if my first way of calling the web service is a recommended way or am I doing anything wrong here? If the first method of calling the web service is wrong, why should it work on all other iOS versions except 8.3?

Method 2:

NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] init];
NSData *postData = [dataToBeSent dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
[urlRequest setHTTPBody:postData];
[urlRequest setHTTPShouldHandleCookies:YES];
[urlRequest setURL:urlOfService];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];

NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
}];
CrazyDeveloper
  • 995
  • 1
  • 13
  • 24

2 Answers2

4

You have to use the second method because of bug(feature?) in iOS 8.3 SDK http://openradar.appspot.com/20498383

-1

I don't see the mistake in your code. But it maybe worth to try using AFNetworking instead.

The library is very complete and gives a lot of flexibility concerning the content-types, asynchronous connections etc... I don't think we can predict the behavior of the new iOS versions since they deprecate a lot of stuff very quickly. AFNetworking is being updated all the time.

Here is the framework, they have examples and everything you could possibly need.

You will probably have to use requestserialization. There are a few examples on the page I linked you to.

Jose Luis
  • 3,307
  • 3
  • 36
  • 53
  • For now I do not want to use AFNetworking in my project. If I use second method it is working fine. I just want to know if it is a bug with NSURLSessionConfiguration API in iOS 8.3. So that I can go ahead and file a bug in radar. – CrazyDeveloper Apr 15 '15 at 15:02