I'm using the swift-2.0 branch on iOS 9 with Xcode 7 beta 2. When trying to set the shared session's headers, it seems that all values gets dismissed:
var headers = ["User-Agent": "MyUserAgent"]
if let authToken = authToken {
headers["Authorization"] = "Bearer \(authToken)"
}
Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders = headers
// Breakpoint here
(lldb) po headers
▿ 2 elements
▿ [0] : 2 elements
- .0 : "User-Agent"
- .1 : "MyUserAgent"
▿ [1] : 2 elements
- .0 : "Authorization"
- .1 : "Bearer XcTp1mg1p8PwQe3KTejkHVGml2ujOsjrQneqkgDKm0iLqv6bGDAfWCJHqSb29D91"
(lldb) po Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders
▿ Optional([Accept-Language: en;q=1.0, Accept-Encoding: gzip;q=1.0,compress;q=0.5, User-Agent: App/com.site.App (0.0.1.0; OS Version 9.0 (Build 13A4280e))])
▿ Some : 3 elements
▿ [0] : 2 elements
- .0 : Accept-Language
- .1 : "en;q=1.0"
▿ [1] : 2 elements
- .0 : Accept-Encoding
- .1 : "gzip;q=1.0,compress;q=0.5"
▿ [2] : 2 elements
- .0 : User-Agent
- .1 : "App/com.site.App (0.0.1.0; OS Version 9.0 (Build 13A4280e))"
I expected to see the Authorization token as well as User-Agent=MyUserAgent when inspecting the http headers. What's happening here?
Looks like I didn't read the documentation enough:
Modifying Session Configuration
var defaultHeaders = Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders ?? [:]
defaultHeaders["DNT"] = "1 (Do Not Track Enabled)"
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.HTTPAdditionalHeaders = defaultHeaders
let manager = Alamofire.Manager(configuration: configuration)
Discussion
Changing mutable values within the configuration object has no effect on the current session, but you can create a new session with the modified configuration object.
So I guess I have to create a new manager with a custom configuration. I'll try that and close the issue if it works as expected.
And it works! Sorry for the noise.
hi, I'm running into the same issue - trying to update the Manager.defaultHTTPHeaders so I don't have to set on every new request. Can I just update the defaultHttpHeaders?
@mypark You can't change the default http headers of the shared manager, you have to create a new manager with the code above and use it for all your requests. It has the same interface than Manager.
@ldiqual Thank you so much it resolved the issue
@ldiqual I have the same problem but still doesn't work, this was working on previous version
private func setHeaderToken(token:String){
var defaultHeaders = Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders ?? [:]
defaultHeaders["Authorization"] = "Bearer " + token
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.HTTPAdditionalHeaders = defaultHeaders
let manager = Alamofire.Manager(configuration: configuration)
}
@cesar-oyarzun-m I got the same problem... I try several way and sample, but the "Authorization" header are not added to the request. I "snif' the local request with Charles and I do not see it in the raw header. Do you find a solution? I am on the 3.1.2
thanks
@pdsavard, you need to pass Authorization headers as part of the Request. Not through the NSURLSessionConfiguration. See the README for more info.
@cnoon what are your thoughts on updating the README to more accurately reflect this?
This is not recommended for
AuthorizationorContent-Typeheaders. Instead, useURLRequestConvertibleandParameterEncoding, respectively.
As @cesar-oyarzun-m said, this used to work despite the recommendation to do otherwise but now it seems as if it's more than that.
IMO the statement is accurate @paulyoung. If you think there's room for improvement, then by all means, please submit and PR and we'll certainly take a look! 👍🏼
Most helpful comment
Looks like I didn't read the documentation enough:
And from https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSession_class/#//apple_ref/occ/instp/NSURLSession/configuration :
So I guess I have to create a new manager with a custom configuration. I'll try that and close the issue if it works as expected.