Last active
June 6, 2017 13:59
-
-
Save manishpathak99/348f2eb0167c0ff6e12ecd667612bc9b to your computer and use it in GitHub Desktop.
Print JSON response on console using ALAMOFIRE swift 3.0+
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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