Skip to content

Instantly share code, notes, and snippets.

@manishpathak99
Last active June 6, 2017 13:59
Show Gist options
  • Save manishpathak99/348f2eb0167c0ff6e12ecd667612bc9b to your computer and use it in GitHub Desktop.
Save manishpathak99/348f2eb0167c0ff6e12ecd667612bc9b to your computer and use it in GitHub Desktop.
Print JSON response on console using ALAMOFIRE swift 3.0+
import Foundation
import Alamofire
extension Alamofire.DataRequest {
func responseDebugPrint() -> Self {
return responseJSON() {
response in
if let JSON = response.result.value,
let JSONData = try? JSONSerialization.data(withJSONObject: JSON, options: .prettyPrinted),
let prettyString = NSString(data: JSONData, encoding: String.Encoding.utf8.rawValue) {
print(prettyString)
} else if let error = response.result.error {
print("Error Debug Print: \(error.localizedDescription)")
}
}
}
}
/****************************/
// Use case : We can use like this
/****************************/
Alamofire.request(...).responseObject {
//Code here
}.responseDebugPrint()
/************* OR ***************/
Alamofire.request(url, method: .get, parameters: parameters, headers: headers)
.validate()
.responseObject { (response: DataResponse<T>) in
self.pendingRequests.removeValue(forKey: endPoint)
completion!(response)
if(NetworkConfig.loggingEnable) {
debugPrint("************* printing REQUEST parameter and Headers *************")
debugPrint("RESPONSE : \(response.debugDescription)")
}
}.responseDebugPrint()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment