我正在调用一个API,偶尔会抛出HTTP 403错误,响应体可以以json的形式提供一些额外的信息,但是我无论如何都似乎无法从Alamofire响应对象中获取这些信息。如果通过chrome访问API,开发者工具中可以看到这些信息。以下是我的代码:
Alamofire.request(mutableURLRequest).validate().responseJSON() {
(response) in
switch response.result {
case .Success(let data):
if let jsonResult = data as? NSDictionary {
completion(jsonResult, error: nil)
} else if let jsonArray = data as? NSArray {
let jsonResult = ["array" : jsonArray]
completion(jsonResult, error: nil)
}
case .Failure(let error):
//error tells me 403
//response.result.data can't be cast to NSDictionary or NSArray like
//the successful cases, how do I get the response body?
}
我已查询了响应附加的几乎所有对象,但在HTTP错误的情况下似乎没有返回响应主体。这里是否有解决方法或我遗漏的内容?
String(data: data, encoding: String.Encoding.utf8)
。 - DilapidusSwiftyJSON
,则可以执行以下操作:JSON(response.data!)
。 - Isaac Bosca