我想在Alamofire请求的失败部分中找到一些状态回复。
以下代码:
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
//
}
}
}
由于某种原因返回500错误,但这不是来自API,而是来自代码本身,但是它导致了什么?我无法弄清楚如何正确获取Status code
个回复。
答案 0 :(得分:0)
您可以从响应中访问状态代码,如下所示:
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")
}
}