johnson23
johnson23

Reputation: 296

Adding headers to Alamofire request (with ephemeral session)

I'm trying to add headers into AlamoFire requests.

It works well when I use the following code:

let manager = Manager.sharedInstance
manager.session.configuration.HTTPAdditionalHeaders = [
    "Authorization": "Bearer \(accessToken!)" ]

However, when I need to user an ephemeral session it does not work with the following code.

let configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration()
let manager = Manager(configuration: configuration)
manager.session.configuration.HTTPAdditionalHeaders = [
    "Authorization": "Bearer \(accessToken!)" ]

Am I missing something?

Thanks in advance,

Upvotes: 4

Views: 1546

Answers (2)

Saumil Shah
Saumil Shah

Reputation: 2349

 func HTTPHeader(param : NSDictionary)
    {
        var username: AnyObject? = param[kAPIUsername]
        var password: AnyObject? = param[kAPIPassword]

        var str = "\(username!):\(password!)"

        let base64Encoded = BaseVC.sharedInstance.encodeStringToBase64(str)

        Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders = [
            "Authorization": "Basic \(base64Encoded)",
        ]
    }


  func encodeStringToBase64(str : String) -> String
    {
        // UTF 8 str from original
        // NSData! type returned (optional)
        let utf8str = str.dataUsingEncoding(NSUTF8StringEncoding)

        // Base64 encode UTF 8 string
        // fromRaw(0) is equivalent to objc 'base64EncodedStringWithOptions:0'
        // Notice the unwrapping given the NSData! optional
        // NSString! returned (optional)
        let base64Encoded = utf8str!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))

        return base64Encoded;
    }


 func GET()
    {
        HTTPHeader(param, token: token)

        request(.GET, API URL , encoding: .JSON)
            .progress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in
                //progress

            }
            .responseJSON {

                (request, response, json, error) -> Void in

            //get response here 
        }
    }

might be help with link: how to use Alamofire with custom headers

Upvotes: 2

VincentS
VincentS

Reputation: 592

You have to ensure your manager is retained, you could do it by setting it as a stored property. You'll find more informations here: https://github.com/Alamofire/Alamofire/issues/157

let customManager: Manager?

init() {
    let configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration()
    self.customManager = Manager(configuration: configuration)
    self.customManager!.session.configuration.HTTPAdditionalHeaders = [
    "Authorization": "Bearer \(accessToken!)" ]
}


func yourFunc() {
    let URL = NSURL(string: identityURL)
    var mutableURLRequest = NSMutableURLRequest(URL: URL!)

    let request = self.customManager!.request(mutableURLRequest)

    request.responseJSON {
        (request, response, data, error) in
        if let response = response {
             // Your code
        }
}

Upvotes: 4

Related Questions