我正在使用 Alamofire 发出 api 请求。当我的 api 失败时,它会给出 html 响应,因此 alamofire 会返回如下错误消息
Reponse ->FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 1." UserInfo={NSDebugDescription=Invalid value around character 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。请告诉我该怎么做。
最佳答案
将响应数据转换成字符串,如下所示
if let _ = result.error {
print("--------- Error -------")
if let responseData = result.data {
let htmlString = String(data: responseData, encoding: .utf8)
print(htmlString!)
}
}
关于ios - 如何在 alamofire 中快速打印错误 html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49421267/