Skip to content

Instantly share code, notes, and snippets.

@AlvaroOlave
AlvaroOlave / .lldbinit
Last active July 24, 2024 14:25
Custom LLDB command that generates a cURL command from any URLRequest in Swift
command regex curl 's/(.+)/expr -l Swift -O -- let anyRequest = %1; if let unwrappedRequest = (anyRequest as? URLRequest), let url = unwrappedRequest.url { var baseCommand = #"curl "\#(url.absoluteString)""#; let quote = String(UnicodeScalar(UInt8(39))); if unwrappedRequest.httpMethod == "HEAD" { baseCommand += " --head"; }; var command = [baseCommand]; if let method = unwrappedRequest.httpMethod, (method != "GET" && method != "HEAD") { command.append("-X \(method)"); }; if let headers = unwrappedRequest.allHTTPHeaderFields { for (key, value) in headers where key != "Cookie" { command.append("-H \(quote)\(key): \(value)\(quote)"); }; }; if let data = unwrappedRequest.httpBody, let body = String(data: data, encoding: .utf8) { command.append("-d \(quote)\(body)\(quote)"); }; print(command.joined(separator: " \\\n\t"));}/'