Skip to content

Instantly share code, notes, and snippets.

@shaps80
Last active December 23, 2023 17:43
Show Gist options
  • Star 38 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save shaps80/ba6a1e2d477af0383e8f19b87f53661d to your computer and use it in GitHub Desktop.
Save shaps80/ba6a1e2d477af0383e8f19b87f53661d to your computer and use it in GitHub Desktop.
Generates a cURL command representation of a URLRequest in Swift.
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)""#
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")
}
}
@shaps80
Copy link
Author

shaps80 commented Feb 13, 2017

Quick attribution: I wrote this implementation myself, but it was inspired from a few years back when I saw @dantoml implement this in a project we were working on. One of the most useful pieces of code and I use it almost every day.

@junestchick
Copy link

Hi shaps80, can you hellp me more about curl in swift way?

@aliabbas90
Copy link

aliabbas90 commented Aug 12, 2019

Hi @shaps80

I added double quotes around url.absoluteString, otherwise the curl command in terminal doesn't work correctly (especially if there are multiple query parameters).
so the line with baseCommand looks like:

>= swift 5:
var baseCommand = #"curl "\#(url.absoluteString)""#

Older versions of swift:
var baseCommand = "curl \"\(url.absoluteString)\""

Ali

@jrtibbetts
Copy link

This is super helpful! Thanks.

@shaps80
Copy link
Author

shaps80 commented Sep 3, 2019

No worries, glad you found it useful.

@heart
Copy link

heart commented Apr 20, 2020

Thankyou this is helpful

@shaps80
Copy link
Author

shaps80 commented Apr 21, 2020

Thankyou this is helpful

👊

@shaps80
Copy link
Author

shaps80 commented Apr 21, 2020

Hi @shaps80

I added double quotes around url.absoluteString, otherwise the curl command in terminal doesn't work correctly (especially if there are multiple query parameters).
so the line with baseCommand looks like:

>= swift 5:
var baseCommand = #"curl "\#(url.absoluteString)""#

Older versions of swift:
var baseCommand = "curl \"\(url.absoluteString)\""

Ali

👊

@heart
Copy link

heart commented Apr 22, 2020

You can write as

#if swift(>=5.0)
  var baseCommand = #"curl "\#(url.absoluteString)""#
#else
  var baseCommand = "curl \"\(url.absoluteString)\""
#endif

it will check the swift version at compile time

@shaps80
Copy link
Author

shaps80 commented Apr 22, 2020

You can write as

#if swift(>=5.0)
  var baseCommand = #"curl "\#(url.absoluteString)""#
#else
  var baseCommand = "curl \"\(url.absoluteString)\""
#endif

it will check the swift version at compile time

👊

@AlvaroOlave
Copy link

I just post a gist based on gist that is a custom LLDB command that generates a cURL command from any URLRequest in Swift. Hope find it interesting! :)

https://gist.github.com/AlvaroOlave/2ddf2b1e99d4477a51146530b7801aa0

@shaps80
Copy link
Author

shaps80 commented Jul 3, 2023

@AlvaroOlave I love this! No need to include the code in every project! Amazing :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment