Skip to content

Instantly share code, notes, and snippets.

@klauslanza
Last active April 3, 2018 06:50
Show Gist options
  • Save klauslanza/a110473b14f3ebd0922f43429206b225 to your computer and use it in GitHub Desktop.
Save klauslanza/a110473b14f3ebd0922f43429206b225 to your computer and use it in GitHub Desktop.
URLRequest debugging -> Print the CURL
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