Last active
April 28, 2022 09:55
-
-
Save kean/cacb6d2e6bafa912bf130d3db1c2f116 to your computer and use it in GitHub Desktop.
test-octokit 2
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 { | |
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") | |
} | |
} |
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
test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment