Authenticated HTTP Swift Alamofire Request

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:

runtime error

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!

0
source share
2 answers

Thus, most of your code looks solid.

The error leads me to think that CFNetwork finds it difficult to figure out how to calculate the security space for a task. I also assume that you are getting a basic auth call since you are attaching an Authorization header.

Delving into your logic a bit more, I thought you were not attaching your token to the string correctly inside the Authorization header. You need to do the following.

 defaultHeaders["Authorization"] = "bearer \(token!)" 

Otherwise, your Authorization header value will include Optional(value) instead of value .

This is the only problem that I see at the moment. If you could give this try and comment, that would be great. I will update my answer accordingly if this does not solve your problem.

Good luck

+2
source

You can add headers to your query with Alamofire 2 and Swift 2 .

Example: go to example

0
source

All Articles