使用alamofire如何将响应作为原始字符串

zzh*_*ads 7 json swift alamofire

在我的项目中使用Alamofire库.如果失败我想从服务器获取所有可能的信息,为什么,不仅是Alamofire制作的Error对象,而是完整的原始字符串或json.我怎么能得到它?

小智 24

这是Alamofire官方网站上的一个演示.response.response.data即使请求出错,您也可以从服务器获取所有JSON或字符串.

Alamofire.request("https://httpbin.org/get").response { response in
    print("Request: \(response.request)")
    print("Response: \(response.response)")
    print("Error: \(response.error)")
    print("Timeline: \(response.timeline)")

    if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
        print("Data: \(utf8Text)")
    }
}
Run Code Online (Sandbox Code Playgroud)

response.error用于简化代码.