Skip to content

Instantly share code, notes, and snippets.

@Rukh
Forked from abhi21git/ExtensionURLRequest.swift
Last active March 28, 2024 09:53
Show Gist options
  • Save Rukh/6a0655842e1db5fe48fcd699221d7b68 to your computer and use it in GitHub Desktop.
Save Rukh/6a0655842e1db5fe48fcd699221d7b68 to your computer and use it in GitHub Desktop.
Swift cURL Printer
//
// URLRequest+cURL.swift
//
// Created by Dmitry Gulyagin on 19/02/2024.
//
import struct Foundation.URLRequest
public extension URLRequest {
func cURL() -> String {
let cURL = "curl -f"
let method = "-X \(self.httpMethod ?? "GET")"
let url = url.flatMap { "--url '\($0.absoluteString)'" }
let header = self.allHTTPHeaderFields?
.map { "-H '\($0): \($1)'" }
.joined(separator: " ")
let data: String?
if let httpBody, !httpBody.isEmpty {
if let bodyString = String(data: httpBody, encoding: .utf8) { // json and plain text
let escaped = bodyString
.replacingOccurrences(of: "'", with: "'\\''")
data = "--data '\(escaped)'"
} else { // Binary data
let hexString = httpBody
.map { String(format: "%02X", $0) }
.joined()
data = #"--data "$(echo '\#(hexString)' | xxd -p -r)""#
}
} else {
data = nil
}
return [cURL, method, url, header, data]
.compactMap { $0 }
.joined(separator: " ")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment