Skip to content

Instantly share code, notes, and snippets.

@vkostechko
Created March 13, 2018 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vkostechko/bef5ac62a428d5a4c2d308bdd5136bb9 to your computer and use it in GitHub Desktop.
Save vkostechko/bef5ac62a428d5a4c2d308bdd5136bb9 to your computer and use it in GitHub Desktop.
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