Skip to content

Instantly share code, notes, and snippets.

@hjazz
Last active October 10, 2016 08:54
Show Gist options
  • Save hjazz/fa6ae0c047c075662fc0a9d806d2c873 to your computer and use it in GitHub Desktop.
Save hjazz/fa6ae0c047c075662fc0a9d806d2c873 to your computer and use it in GitHub Desktop.
Request log with curl (swift3)
extension URLRequest {
var curlDescription: String {
get {
var displayString = "curl -v -X \(self.httpMethod!)"
if let absoluteUrl = self.url?.absoluteString {
displayString += " '\(absoluteUrl)'"
}
if let allHTTPHeaderFields = self.allHTTPHeaderFields {
let allHeadersKeys = Array(allHTTPHeaderFields.keys)
let sortedHeadersKeys = allHeadersKeys.sorted()
for key in sortedHeadersKeys {
displayString += " -H '\(key): \(self.value(forHTTPHeaderField: key)!)'"
}
}
if self.httpMethod == "POST" ||
self.httpMethod == "PUT" ||
self.httpMethod == "PATCH", let body = self.httpBody {
if let bodyString = String(data: body, encoding: String.Encoding.utf8) {
displayString += " -d '\(bodyString)'"
}
}
return displayString
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment