Skip to content

Instantly share code, notes, and snippets.

@juliengdt
Created September 1, 2016 09:24
Show Gist options
  • Save juliengdt/c72b064a57e13f0bb89e2f6141d08ecb to your computer and use it in GitHub Desktop.
Save juliengdt/c72b064a57e13f0bb89e2f6141d08ecb to your computer and use it in GitHub Desktop.
Pretty print extension for Alamofire Request
extension Alamofire.Request {
func responseDebugPrint() -> Self {
return responseJSON() {
response in
if let JSON: AnyObject = response.result.value,
JSONData = try? NSJSONSerialization.dataWithJSONObject(JSON, options: .PrettyPrinted),
prettyString = NSString(data: JSONData, encoding: NSUTF8StringEncoding) {
print(prettyString)
} else if let error = response.result.error {
print("Error Debug Print: \(error.localizedDescription)")
}
}
}
}
@manishpathak99
Copy link

Dude, it's giving me error on line 3 , use of unresolved identifier "responseJSON"

@smaljaar
Copy link

extension DataRequest {

    @discardableResult
    func prettyPrintedJsonResponse() -> Self {
        return responseJSON { (response) in
            switch response.result {
            case .success(let result):
                if let data = try? JSONSerialization.data(withJSONObject: result, options: .prettyPrinted),
                    let text = String(data: data, encoding: .utf8) {
                    print("📗 prettyPrinted JSON response: \n \(text)")
                }
            case .failure: break
            }
        }
    }
}

@aliwaseem72
Copy link

@smaljaar How to use it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment