Search code examples
alamofire

Alamofire 2 requests succeeds even when given a bad http statuscode in the response


Now that I've upgraded to Alamofire 2, I've noticed that the callback considers responses to be a success even if it returns a non 200 or 4xx http response.

Is that the intended behavior? What's the recommended way of checking for the bad responses - just manually checking the response.statusCode?

Alamofire.request(.GET, "http://somesite.org/private")
         .responseJSON { _, _, result in
             print(result.isSuccess) // is true even if it's a 403 or 404 response
         }

Solution

  • I think this is default behavior. I think you have to validate the response. Everything outside the provided status codes will be handled as failure.

    Alamofire.request(.GET, "http://somesite.org/private")
         .validate(statusCode: 200..<300)
         .responseJSON { _, _, result in
             print(result.isSuccess) // is true even if it's a 403 or 404 response
         }