我正在向Alamofire提出api请求。当我的api失败时,它会给出html响应,因此alamofire会返回如下错误消息
回复 - >失败: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain = NSCocoaErrorDomain Code = 3840)“字符1周围的值无效。”UserInfo = {NSDebugDescription =字符1周围的值无效。})
我试过下面的代码
Alamofire.request(absolutePath(forApi: functionName), method: apiMethod, parameters: parameters, encoding: JSONEncoding.default, headers: defaultHeader())
.responseJSON { result in
DILog.print(items: "URL -> \(self.absolutePath(forApi: functionName))")
if let _ = parameters {
DILog.print(items: "Parameters ->\(String(describing: parameters)) ")
}
DILog.print(items: "Headers ->\(self.defaultHeader()) ")
DILog.print(items: "Reponse ->\(result) ")
DILog.print(items: "Reponse1 ->\(result.value) ")
DILog.print(items: "Reponse Code ->\(result.response?.statusCode) ")
if let errorResponse = result.error {
print(errorResponse.localizedDescription)
failure(self.parseError(error: errorResponse))
}
}
我想将错误响应打印为API返回的html。请告诉我怎么做。
答案 0 :(得分:0)
希望能帮助您打印Alamofire的回复 https://github.com/netguru/ResponseDetective
答案 1 :(得分:0)
将响应数据转换为字符串,如下所示
if let _ = result.error {
print("--------- Error -------")
if let responseData = result.data {
let htmlString = String(data: responseData, encoding: .utf8)
print(htmlString!)
}
}