I'm struggling to get this to work to make a request to my API. It works without tokens, but when I try to add additional headers, everything becomes complicated for me.
The first is structure. one class is called: APIAsyncTask , which makes requests
one class called APIParams , just a data holder for sending parameters to the APIAsyncTask class.
one class called DatabaseAPI , which forces it to create parameters and send them to the APIAsyncTask class.
DatabaseAPI
func someMethod() { let task = APIAsyncTasks() task.registerCallback { (error, result) -> Void in print("Finished task, back at DatabaseAPI") } let params2 = APIParams(request: .GET, apiPath: "Posts/1", apiToken: "4iTX-56w") task.APIrequest(params2) }
APIAsyncTask
This part is intended to fix another error, because the manager was not global, the task was quickly canceled.
var manager : Manager! init(authenticatedRequest : Bool, token: String?) { manager = Alamofire.Manager() print("Pre \(manager.session.configuration.HTTPAdditionalHeaders?.count)") if(authenticatedRequest && 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)") }
After making some decisions, it comes down to this part.
private func GetRequest(url: String!,token : String?, completionHandler: (JSON?, NSURLRequest?, NSHTTPURLResponse?, NSError?) -> () ) -> () { 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
Since the code is now, I get an error message:

Itself gives an answer using Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders , so that should be fine ...
I suspect this is due to multiple threads, according to this blog. Or, since this is something about CFNetwork, could it be because my API does not use SSL? I disabled NSAppTransportSecurity
I'm a little new to fast, so the examples would be much appreciated! Thankyou!