Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 707
zzheads Avatar asked Aug 17 '17 10:08

zzheads


People also ask

Is Alamofire asynchronous?

Everything with Alamofire is asynchronous, which means you'll update the UI in an asynchronous manner: Hide the upload button, and show the progress view and activity view. While the file uploads, you call the progress handler with an updated percent.


Video Answer


1 Answers

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

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

The response.error is used for simplifying your code.

like image 144
pluto Avatar answered Oct 24 '22 21:10

pluto