Skip to content

Instantly share code, notes, and snippets.

@tomaskraina
Last active August 22, 2016 09:06
Show Gist options
  • Save tomaskraina/d644af49e4968de85e34 to your computer and use it in GitHub Desktop.
Save tomaskraina/d644af49e4968de85e34 to your computer and use it in GitHub Desktop.
extension NSURLRequest: DebugPrintable
//
// NSURLRequestExtension.swift
//
// Source: https://gist.github.com/tomaskraina/d644af49e4968de85e34
//
// Created by Tom Kraina on 21/04/2015.
// Copyright (c) 2015 Tom Kraina. All rights reserved.
//
import Foundation
// Inspired by: https://github.com/Blackjacx/NSURLRequest-SHLogging,
extension NSURLRequest {
public func extendedDescription() -> String {
var result = "<\(NSStringFromClass(self.dynamicType)): " + String(format: "%p", self)
result += "; HTTPMethod=\(HTTPMethod ?? "nil"); URL=\(URL); timeoutInterval=\(String(format: "%.1fs", timeoutInterval))> {"
// Add header fields.
if let headers = allHTTPHeaderFields {
result += "\nallHTTPHeaderFields {"
for (key, value) in headers {
result += "\n\t\(key) : '\(value)'"
}
result += "\n}"
}
if let body = HTTPBody {
result += "\nHTTPBody {\n \(String(data: body, encoding: NSASCIIStringEncoding) ?? "")\n}"
}
return result + "\n}"
}
public override var debugDescription: String {
return extendedDescription()
}
public override var description: String {
return extendedDescription()
}
}
@tomaskraina
Copy link
Author

If printed out, it displays something similar to the following:

<NSMutableURLRequest: 0x7fa91977e390; HTTPMethod=POST; URL=https://myapi.com/api/something; timeoutInterval=300.0s> {
allHTTPHeaderFields {
    Accept-Encoding : 'gzip'
    Content-Type : 'application/json'
    Accept : 'application/json'
}
HTTPBody {
    {"key1":"value1"}
}
}

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