NSURLSessionConfiguration HTTPAdditionalHeaders not set

The authorization header is set to NSURLSessionConfiguration , however it is not bound to NSURLSessionDataTask . Is this a bug in the Foundation framework ?

 NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; [configuration setHTTPAdditionalHeaders:@{@"Authorization":@"123"}]; // Initialize session with NSURLSessionConfiguration NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; NSURLSessionDataTask *sessionTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { }]; [sessionTask resume]; 
+6
source share
2 answers

I try to do this in Swift and it works

  var sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration() var xHTTPAdditionalHeaders: [NSObject : AnyObject] = ["X-test":"taly"] sessionConfig.HTTPAdditionalHeaders = xHTTPAdditionalHeaders let session = NSURLSession(configuration: sessionConfig) let task = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in 
+1
source

In the NSURLSessionConfiguration document,

The NSURLSession object is designed to handle various aspects of the HTTP protocol for you. As a result, you should not change the following headers :

Login

Compound

Host

WWW-Authenticate

+5
source

Source: https://habr.com/ru/post/972229/


All Articles