zzheads zzheads -3 years ago 220
JSON Question

Using alamofire how get response as raw string

Using Alamofire library in my project. In case failure I want get all possible information from server why, not only Error object made by Alamofire, but full raw string or json. How I can get it?

Answer Source

Here is a Demo on Alamofire Official website. You can get all the json or string from your server as response.response.data, even the request get an error

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

    if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
        print("Data: \(utf8Text)")

The response.error is used for simplifying your code

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download