Skip to content

Commit 0052388

Browse files
author
Konstantin Kabanov
committed
response body printing
1 parent 2d45dc7 commit 0052388

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

Cartfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
github "Alamofire/Alamofire" ~> 4.0
1+
github "Alamofire/Alamofire" ~> 4.2

Source/NetworkActivityLogger.swift

+17-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ public class NetworkActivityLogger {
141141
}
142142

143143
@objc private func networkRequestDidComplete(notification: Notification) {
144-
guard let userInfo = notification.userInfo,
144+
guard let sessionDelegate = notification.object as? SessionDelegate,
145+
let userInfo = notification.userInfo,
145146
let task = userInfo[Notification.Key.Task] as? URLSessionTask,
146147
let request = task.originalRequest,
147148
let httpMethod = request.httpMethod,
@@ -184,6 +185,21 @@ public class NetworkActivityLogger {
184185
for (key, value) in response.allHeaderFields {
185186
print("\(key): \(value)")
186187
}
188+
189+
guard let data = sessionDelegate[task]?.delegate.data else { break }
190+
191+
do {
192+
let jsonObject = try JSONSerialization.jsonObject(with: data, options: .mutableContainers)
193+
let prettyData = try JSONSerialization.data(withJSONObject: jsonObject, options: .prettyPrinted)
194+
195+
if let prettyString = String(data: prettyData, encoding: .utf8) {
196+
print(prettyString)
197+
}
198+
} catch {
199+
if let string = NSString(data: data, encoding: String.Encoding.utf8.rawValue) {
200+
print(string)
201+
}
202+
}
187203
case .info:
188204
print("\(String(response.statusCode)) '\(requestURL.absoluteString)' [\(String(format: "%.04f", elapsedTime)) s]")
189205
default:

0 commit comments

Comments
 (0)