// // UtilityFunction.swift // // Created by Abhishek Maurya on 23/10/20. // Copyright © 2020. All rights reserved. // import Alamofire import Foundation class UtilityFunction: NSObject { internal static let shared: UtilityFunction = { return UtilityFunction() }() // Call this funcction inside Alomofire.request and pass the data response class func debugApiResponse(_ response: DataResponse, pretty: Bool = false) { print("\n") if let statusCode = response.response?.statusCode { print("Status code: " + String(statusCode)) } if let request = response.request?.cURL(pretty: pretty) { print("Request cURL: \n" + request) } else { if let url = response.request?.url?.absoluteString { print("URL: \n" + url) } if let headers = response.request?.allHTTPHeaderFields { print("Headers: \n\(headers as AnyObject)") } } if let data = response.data { if let json = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers), let jsonData = try? JSONSerialization.data(withJSONObject: json, options: .prettyPrinted), pretty { print("Response JSON pretty-printed: \n" + String(decoding: jsonData, as: UTF8.self)) } else if let jsonString = String(data: data, encoding: .utf8) { print("Response JSON: \n" + jsonString) } if let responseHeader = response.response?.allHeaderFields, responseHeader.keys.count > 0 { var header = "{\(pretty ? "\\\n" : "")" for (key,value) in responseHeader { header += " \"\(key): \(value)\",\(pretty ? "\\\n" : "")" } print("Response Header: \n\(header.dropLast())\(pretty ? "\\\n" : "")}") } } print("\n") } } // If you want to use cURL() with Alomofire request