Created
March 13, 2018 08:56
-
-
Save vkostechko/bef5ac62a428d5a4c2d308bdd5136bb9 to your computer and use it in GitHub Desktop.
URLRequest debugging - http://www.gentlebytes.com/blog/2018/02/28/request-debugging/
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
extension URLRequest { | |
public var curlString: String { | |
// Logging URL requests in whole may expose sensitive data, | |
// or open up possibility for getting access to your user data, | |
// so make sure to disable this feature for production builds! | |
#if !DEBUG | |
return "" | |
#else | |
var result = "curl -k " | |
if let method = httpMethod { | |
result += "-X \(method) \\\n" | |
} | |
if let headers = allHTTPHeaderFields { | |
for (header, value) in headers { | |
result += "-H \"\(header): \(value)\" \\\n" | |
} | |
} | |
if let body = httpBody, !body.isEmpty, let string = String(data: body, encoding: .utf8), !string.isEmpty { | |
result += "-d '\(string)' \\\n" | |
} | |
if let url = url { | |
result += url.absoluteString | |
} | |
return result | |
#endif | |
} | |
} | |
Usage | |
And log it: | |
let request = ... | |
print("\(request.curlString)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment