Skip to content

Instantly share code, notes, and snippets.

@kean
Last active April 28, 2022 09:55
Show Gist options
  • Save kean/cacb6d2e6bafa912bf130d3db1c2f116 to your computer and use it in GitHub Desktop.
Save kean/cacb6d2e6bafa912bf130d3db1c2f116 to your computer and use it in GitHub Desktop.
test-octokit 2
extension URLRequest {
public func cURLDescription() -> String {
guard let url = url, let method = httpMethod else {
return "$ curl command generation failed"
}
var components = ["curl -v"]
components.append("-X \(method)")
for header in allHTTPHeaderFields ?? [:] {
let escapedValue = header.value.replacingOccurrences(of: "\"", with: "\\\"")
components.append("-H \"\(header.key): \(escapedValue)\"")
}
if let httpBodyData = httpBody {
let httpBody = String(decoding: httpBodyData, as: UTF8.self)
var escapedBody = httpBody.replacingOccurrences(of: "\\\"", with: "\\\\\"")
escapedBody = escapedBody.replacingOccurrences(of: "\"", with: "\\\"")
components.append("-d \"\(escapedBody)\"")
}
components.append("\"\(url.absoluteString)\"")
return components.joined(separator: " \\\n\t")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment