title: Authenticated http request swift Alamofire
body: <p>I'm struggling with getting this to work to make request to my API. Without a token works, but when I try to add additional headers, things turn to be complicated, for me.</p>
  
  <p>First, the structure.
  one class called: <strong>APIAsyncTask</strong> that makes the requests</p>
  
  <p>one class called <strong>APIParams</strong>, just a data holder to send parameters to the <em>APIAsyncTask</em> class.</p>
  
  <p>one class called <strong>DatabaseAPI</strong> that makes that builds the parameters, and send that to the <em>APIAsyncTask</em> class.</p>
  
  <p><strong>DatabaseAPI</strong></p>
  
  <pre><code>func someMethod()
  {
      let task = APIAsyncTasks()
      task.registerCallback { (error, result) -&gt; Void in
          print("Finished task, back at DatabaseAPI")
      }
      let params2 = APIParams(request: .GET, apiPath: "Posts/1", apiToken: "4iTX-56w")
      task.APIrequest(params2)
  }
  </code></pre>
  
  <p><strong>APIAsyncTask</strong></p>
  
  <p>This part is for fixing another error, because manager was not global, the task got cancelled quickly.</p>
  
  <pre><code>var manager : Manager!
  
  init(authenticatedRequest : Bool, token: String?)
  {
      manager = Alamofire.Manager()
      print("Pre \(manager.session.configuration.HTTPAdditionalHeaders?.count)")
      if(authenticatedRequest &amp;&amp; token != nil)
      {
          var defaultHeaders = Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders!   
          defaultHeaders["Authorization"] = "bearer \(token)"
          let configuration = Manager.sharedInstance.session.configuration
          configuration.HTTPAdditionalHeaders = defaultHeaders
  
          manager = Alamofire.Manager(configuration: configuration)
      }
      print("Post \(manager.session.configuration.HTTPAdditionalHeaders?.count)")
  }
  </code></pre>
  
  <p>After some decision making, it comes down to this part.</p>
  
  <pre><code> private func GetRequest(url: String!,token : String?, completionHandler: (JSON?, NSURLRequest?, NSHTTPURLResponse?, NSError?) -&gt; () ) -&gt; ()
  {
      print("Begin Get Request")
  
      if(token != nil)//if token is not nil, make authenticated request
      {
          print("just before request: \(manager.session.configuration.HTTPAdditionalHeaders?.count)")
          manager.request(.GET, url, parameters: nil, encoding: .JSON).responseJSON { (request, response, json, error) in
              print("Get Request (authenticated), inside alamofire request")
              var resultJson : JSON?
              if(json != nil)
              {
                  resultJson = JSON(json!)
              }
              completionHandler(resultJson, request, response, error)
          }
      }
      else
      {
       //working part without token
  </code></pre>
  
  <p>So as the code is now, I get an error on completing:</p>
  
  <p><img src="https://i.stack.imgur.com/hEFXk.png" alt="runtime error"></p>
  
  <p>Mattt himself <a href="https://github.com/Alamofire/Alamofire/issues/111" rel="nofollow noreferrer">gives the answer</a> of using <code>Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders</code>
  , so that should be fine...</p>
  
  <p>I suspect it has something to do with the multiple threads, according to <a href="http://www.touch-code-magazine.com/how-to-debug-exc_bad_access/" rel="nofollow noreferrer">this</a> blog. Or, since it is something about CFNetwork, it could be because my API does not use SSL? I disabled NSAppTransportSecurity</p>
  
  <p>I'm kind of new to swift, so examples would be really appreciated! Thankyou!</p>