ios - 如何在 alamofire 中快速打印错误 html?

标签 ios swift alamofire

我正在使用 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/

相关文章:

ios - Flash CS6 和AIR iOS 的远程调试?

ios - 什么时候更新 UITableView 高度 NSLayoutConstraint 最合适

swift - 为什么我应该使用 NSManagedObjectContext 的 Perform() 和 PerformAndWait() 而我可以使用 DispatchQueue.global

json - Alamofire检查返回的数据

ios - Alamofire HTTPS 请求失败

ios - 带有流式 AVAsset -11800 错误的 AVExportSession

ios - 带按钮的一个 ViewController 中的多个音频文件

ios - 如何判断 completionHandler 在哪个队列上执行?

ios - 正在调用 willDisplayHeaderView 但 titleForHeaderInSection 中的文本属性保持不变

ios - 如何使用 Alamofire 4.0 发出 NTML 请求?