Last active
October 10, 2016 08:54
-
-
Save hjazz/fa6ae0c047c075662fc0a9d806d2c873 to your computer and use it in GitHub Desktop.
Request log with curl (swift3)
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 { | |
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