0

I wanna catch some status responses in my failure part of my Alamofire request.

The following code:

let URL = "https://api.foo.bar"
Alamofire.request(URL, method: .post, parameters: parameters).responseObject { (response: DataResponse<UserResponse>) in
    switch response.result {
    case .success:
        // Yea, it worked.
    case .failure(let error):
        print(error)        
    }
    if let httpStatusCode = response.response?.statusCode {
        switch(httpStatusCode) {
        case 418:
            let alert = UIAlertController(title: "Whoops", message: "I't s a Teapot.", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
            break
        default:
            print("DEFAULT...")
            break
            //
        }
    }
}

Returns for some reason a 500 error, but that's not from the API, but from the code itself, but where is it causing it? I can't figure out how to get the Status code responses correctly.

RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70
Sanne V
  • 51
  • 1
  • 1
  • 7

1 Answers1

0

You can access status code from response like this :

switch response.result {
    case .success(let jsonResult):
        // Case result OK
        print(jsonResult)
    case .failure(let error):
        // Case result Fail error
        if let resp = response.response, resp.statusCode == 401 {
            print("401 statusCode")
        }
}
Clegrand
  • 82
  • 4