Alamofire Code=-999 "cancelled"

Created on 6 Nov 2016  路  2Comments  路  Source: Alamofire/Alamofire

I have successfully got keychain for my token and passing it to AccessTokenAdapter class shown below. http127.0.0.1:8000/api2/projects/?format=json is passed as projectsURL.
however, from print(error), my Xcode shows error like Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLKey=http://127.0.0.1:8000/api2/projects/?format=json, NSLocalizedDescription=cancelled, NSErrorFailingURLStringKey=http127.0.0.1:8000/api2/projects/?format=json}

Any ideas?
Alamofire 4.0
Keychain
Xcode 8.1
Swift3
Using JWT for authentication
Using Postman with header, key = "Authentication", value = "JWT (token generated here)" works fine

    class AccessTokenAdapter: RequestAdapter {
        private let accessToken: String

        init(accessToken: String) {
            self.accessToken = accessToken
        }

        func adapt(_ urlRequest: URLRequest) throws -> URLRequest {
            var urlRequest = urlRequest
         //   print("JWT \(accessToken)")
            urlRequest.setValue("JWT \(accessToken)", forHTTPHeaderField: "Authorization")

            return urlRequest
        }
    }


    let sessionManager = SessionManager()
    sessionManager.adapter = AccessTokenAdapter(accessToken: self.keychain["token"]!)

    sessionManager.request(self.projectsURL, method: .get, encoding: JSONEncoding.default).responseJSON{ response in
        switch response.result {
        case .success:
            print("yey I made it")
        case .failure(let error):
            print(error)
        }
    }
support

Most helpful comment

Errors like this are usually caused by the SessionManager you created falling out of scope and being deinitd, causing your requests to be cancelled. Keep a reference to it, like the singleton used by the Alamofire methods, and it will work just fine.

All 2 comments

Just solved.

    let url = URL(string: "http://127.0.0.1:8000/api2/projects/?format=json")
    var urlRequest = URLRequest(url:url!)
    urlRequest.httpMethod = HTTPMethod.get.rawValue
    urlRequest.addValue("JWT \(self.keychain["token"]!)", forHTTPHeaderField: "Authorization")
    urlRequest.addValue("application/json", forHTTPHeaderField: "Accept")
    Alamofire.request(urlRequest)
        .responseJSON { response in
            debugPrint(response)
    }

Errors like this are usually caused by the SessionManager you created falling out of scope and being deinitd, causing your requests to be cancelled. Keep a reference to it, like the singleton used by the Alamofire methods, and it will work just fine.

Was this page helpful?
0 / 5 - 0 ratings