Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alamofire Code=-999 "cancelled" #1757

Closed
XXUeo opened this issue Nov 6, 2016 · 2 comments
Closed

Alamofire Code=-999 "cancelled" #1757

XXUeo opened this issue Nov 6, 2016 · 2 comments
Assignees
Labels

Comments

@XXUeo
Copy link

XXUeo commented Nov 6, 2016

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)
        }
    }
@XXUeo
Copy link
Author

XXUeo commented Nov 6, 2016

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)
    }

@jshier
Copy link
Contributor

jshier commented Nov 12, 2016

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.

@jshier jshier closed this as completed Nov 12, 2016
@jshier jshier added the support label Nov 12, 2016
@jshier jshier self-assigned this Nov 12, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants