Skip to content

Instantly share code, notes, and snippets.

@ollieatkinson
Last active September 17, 2020 14:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ollieatkinson/13056d365d845831c58044182babbd2c to your computer and use it in GitHub Desktop.
Save ollieatkinson/13056d365d845831c58044182babbd2c to your computer and use it in GitHub Desktop.
Generate cURL command's from URLRequest's
//: Playground - noun: a place where people can play
import Foundation
extension URLRequest {
/// The cURL representation of the URLRequest, useful for debugging and executing requests outside of the app.
public var cURLCommand: String {
var command = "curl"
if let httpMethod = httpMethod {
command.append(commandLineArgument: "-X \(httpMethod)")
}
if let httpBody = httpBody, httpBody.count > 0 {
let bodyString = [ ("\\", "\\\\"), ("`", "\\`"), ("\"", "\\\""), ("$", "\\$") ].reduce(String(data: httpBody, encoding: .utf8)) {
return $0?.replacingOccurrences(of: $1.0, with: $1.1)
}!
command.append(commandLineArgument: "-d \"\(bodyString)\"")
}
if let acceptEncoderHeader = allHTTPHeaderFields?["Accept-Encoding"], (acceptEncoderHeader as NSString).range(of: "gzip").location != NSNotFound {
command.append(commandLineArgument: "--compressed")
}
if let url = url, let cookies = HTTPCookieStorage.shared.cookies(for: url), cookies.count > 0 {
let cookieCommand = cookies.map {
"\($0.name)=\($0.value);"
}.joined()
command.append(commandLineArgument: "--cookie \"\(cookieCommand)\"")
}
if let allHTTPHeaderFields = allHTTPHeaderFields {
for (header, value) in allHTTPHeaderFields {
command.append(commandLineArgument: "-H '\(header): \(value.replacingOccurrences(of: "\'", with: "\\\'"))'")
}
}
if let url = url {
command.append(commandLineArgument: "\"\(url.absoluteString)\"")
}
return command
}
}
fileprivate extension String {
mutating func append(commandLineArgument: String) {
append(" \(commandLineArgument.trimmingCharacters(in: CharacterSet.whitespaces))")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment