Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURLSessionConfiguration HTTPAdditionalHeaders not set

Authorization header is set in NSURLSessionConfiguration, however it is not attached to NSURLSessionDataTask. Is this a bug in 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];
like image 896
Armands Antans Avatar asked Jul 15 '14 06:07

Armands Antans


2 Answers

In the NSURLSessionConfiguration document,

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

Authorization

Connection

Host

WWW-Authenticate

like image 89
Dongjin Suh Avatar answered Nov 14 '22 00:11

Dongjin Suh


I try 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
like image 24
Jakub Dubrovsky Avatar answered Nov 14 '22 01:11

Jakub Dubrovsky