Skip to content

Instantly share code, notes, and snippets.

@shaps80
Last active July 18, 2025 10:29

Revisions

  1. shaps80 revised this gist May 4, 2020. 1 changed file with 0 additions and 4 deletions.
    4 changes: 0 additions & 4 deletions cURL+Request.swift
    Original file line number Diff line number Diff line change
    @@ -32,8 +32,4 @@ extension URLRequest {
    return command.joined(separator: " \\\n\t")
    }

    init?(curlString: String) {
    return nil
    }

    }
  2. shaps80 revised this gist May 4, 2020. 1 changed file with 12 additions and 10 deletions.
    22 changes: 12 additions & 10 deletions cURL+Request.swift
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,20 @@
    import Foundation

    extension URLRequest {

    /**
    Returns a cURL command representation of this URL request.
    */
    public var curlString: String {
    guard let url = url else { return "" }
    var baseCommand = "curl \(url.absoluteString)"
    var baseCommand = #"curl "\#(url.absoluteString)""#

    if httpMethod == "HEAD" {
    baseCommand += " --head"
    }

    var command = [baseCommand]

    if let method = httpMethod, method != "GET" && method != "HEAD" {
    command.append("-X \(method)")
    }
    @@ -22,16 +24,16 @@ extension URLRequest {
    command.append("-H '\(key): \(value)'")
    }
    }

    if let data = httpBody, let body = String(data: data, encoding: .utf8) {
    command.append("-d '\(body)'")
    }

    return command.joined(separator: " \\\n\t")
    }

    init?(curlString: String) {
    return nil
    }
    }

    }
  3. shaps80 created this gist Feb 13, 2017.
    37 changes: 37 additions & 0 deletions cURL+Request.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    extension URLRequest {

    /**
    Returns a cURL command representation of this URL request.
    */
    public var curlString: String {
    guard let url = url else { return "" }
    var baseCommand = "curl \(url.absoluteString)"

    if httpMethod == "HEAD" {
    baseCommand += " --head"
    }

    var command = [baseCommand]

    if let method = httpMethod, method != "GET" && method != "HEAD" {
    command.append("-X \(method)")
    }

    if let headers = allHTTPHeaderFields {
    for (key, value) in headers where key != "Cookie" {
    command.append("-H '\(key): \(value)'")
    }
    }

    if let data = httpBody, let body = String(data: data, encoding: .utf8) {
    command.append("-d '\(body)'")
    }

    return command.joined(separator: " \\\n\t")
    }

    init?(curlString: String) {
    return nil
    }

    }