简体   繁体   English

如何在Alamofire中快速打印错误html?

[英]How to print error html in alamofire in swift?

I am making api request with Alamofire.When my api failes it gives html response so alamofire return a error message as below 我正在用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.})) 响应->失败:responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(错误域= NSCocoaErrorDomain代码= 3840“字符1周围的无效值。” UserInfo = {NSDebugDescription =字符1周围的无效值))

I tried below code 我尝试下面的代码

   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))
                }
}

I want to print the error response as html returned by API. 我想将错误响应打印为API返回的HTML。 Please tell me how to do that. 请告诉我该怎么做。

Convert the response data into string, like below 将响应数据转换为字符串,如下所示

if let _ = result.error {
  print("--------- Error -------")
  if let responseData = result.data {
    let htmlString = String(data: responseData, encoding: .utf8)
    print(htmlString!)
  }
}

希望它对您从Alamofire打印响应有帮助https://github.com/netguru/ResponseDetective

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM