Last active
April 3, 2018 06:50
-
-
Save klauslanza/a110473b14f3ebd0922f43429206b225 to your computer and use it in GitHub Desktop.
URLRequest debugging -> Print the CURL
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 { | |
#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 | |
} | |
} | |
// let request = ... | |
// print("\(request.curlString)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment