Alamofire: isSuccess always true, even with error code

Created on 11 Oct 2015  ·  3Comments  ·  Source: Alamofire/Alamofire

The server is sending response code 404, but it looks like the framework is interpreting it as a success.

Code:

Alamofire.request(.GET, Constants.Path.rootUrl + "/api/users", parameters: ["username" : usernameString, "limit":3] , headers: ["tb-token" : userToken!])
            .responseJSON { response in


                print(response.request)  // original URL request
                print(response.response) // URL response
                print(response.data)     // server data
                print(response.result)   // result of response serialization


                let results = response.result

                if let JSON = response.result.value {

                    if response.result.isSuccess == true {
                        print("Success with JSON: \(JSON)")
                        self.tableView.hidden = false

                        self.resultsDict = results.value as! NSArray

                        let resultsNumber = results.value!.count

                        self.numberOfResults.text = "Results(\(resultsNumber))"
                        self.numberOfResults.hidden = false

                        self.resultsN = resultsNumber
                        self.loadedMode = true


                        self.tableView.reloadData()
                        self.tableView.hidden = false
                    }
                }
                    else {print("error")}

        }

isSuccess is always true, whatever code the server is sending back.

question response serializer

All 3 comments

By default Alamofire registers any response that makes it through the response serializer (in your example, the JSONResponseSerializer used by .responseJSON) as a success. If you wish to validate the response code of your request, just add a .validate() to your request chain. This will ensure that your response code is between 200 and 299, inclusive. You can also customize validation using the methods described in the documentation.

In your case:

Alamofire.request(.GET, Constants.Path.rootUrl + "/api/users", parameters: ["username" : usernameString, "limit":3] , headers: ["tb-token" : userToken!])
            .validate()
            .responseJSON { response in }

Thank you, worked like a charm!

Thank you it, worked. Using .validate() the response is validated.
But there is another problem, using .validate() I dont get the response value: response.result.value.
Is there any solution, or workaround?
Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Tulleb picture Tulleb  ·  3Comments

matthijsotterloo picture matthijsotterloo  ·  3Comments

solomon23 picture solomon23  ·  3Comments

tobiasoleary picture tobiasoleary  ·  3Comments

hengchengfei picture hengchengfei  ·  3Comments