I'm working on transferring my project from AFNetworking to Alamofire. Really like the project. However, I'm receiving this error when attempting to make a GET request.
Here's some example code:
class func listCloudCredntials(onlyNew onlyNew: Bool = true, includePending: Bool = true) -> Request {
let parameters: [String: AnyObject] = includePending ? ["include_pending": "true"] : [:]
let urlString = "https://myapp-staging.herokuapp.com/api/1/credntials"
let token = SSKeychain.storedToken()
let headers: [String: String] = ["Authorization": "Bearer \(token)"]
return Alamofire.request(.GET, urlString, parameters: parameters, encoding: .JSON, headers: headers)
}
Then, in my VC:
listCloudCredntials().validate().responseJSON() {
(response: Response<AnyObject, NSError>) in
switch response.result {
case .Success(let result):
printCR("success: \(result)")
case .Failure(let error):
printCR("error: \(error)")
}
}
Here's the error I'm running into:
Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={NSUnderlyingError=0x14e9c77f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={_kCFStreamErrorCodeKey=-4, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=https://myapp-staging.herokuapp.com/api/1/credntials, NSErrorFailingURLKey=https://myapp-staging.herokuapp.com/api/1/credntials, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-4, NSLocalizedDescription=The network connection was lost.}
I've tried running on the iOS Simulator with 1OS 8.4 and iOS 9.1, as well as my iPhone 6S running iOS 9.1.
What am I doing wrong?
--------EDIT--------
To clarify, this function works just fine with AFNetworking.
Here's the debugprint(request) result (it's a GET request):
$ curl -i \
-H "Authorization: Bearer some-JWT-Security-Token" \
-H "Content-Type: application/json" \
-H "Accept-Language: en-US;q=1.0" \
-H "Accept-Encoding: gzip;q=1.0,compress;q=0.5" \
-H "User-Agent: Company/com.Company.AppName (1081; OS Version 9.2 (Build 13C75))" \
-d "{\"include_pending\":\"true\"}" \
"https://appname-staging.herokuapp.com/api/1/credntials"
I have to change curl -i to curl -X GET in order for the curl to return successfully.
Here is another call that I'm required to make in the app that works with no issues.
curl -i \
-X POST \
-H "Content-Type: application/json" \
-H "Accept-Language: en-US;q=1.0" \
-H "Accept-Encoding: gzip;q=1.0,compress;q=0.5" \
-H "User-Agent: Company/com.Company.AppName (1081; OS Version 9.2 (Build 13C75))" \
-d "{\"token\":\"UserSessionTokenString\"}" \
"https://appname-staging.herokuapp.com/api/1/authenticate/user"
Could it be something with the GET vs. POST?
Resolved! I was using JSON encoding for a GET request. Here's the answer: Alamofire Request error only on GET requests
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With