Skip to content

Instantly share code, notes, and snippets.

@meysampg
Created February 14, 2022 12:59
Show Gist options
  • Save meysampg/b40a11dd62d667911da7cf99dd9e7c67 to your computer and use it in GitHub Desktop.
Save meysampg/b40a11dd62d667911da7cf99dd9e7c67 to your computer and use it in GitHub Desktop.
Convert HttpURLConnection to Curl Command in Kotlin
fun toCurl(connection: HttpURLConnection, body: ByteArray): String {
var builder = "curl -v "
builder += "-X " + connection.requestMethod + " \\\n "
for (entry in connection.requestProperties) {
builder += "-H \"" + entry.key + ":"
for (value in entry.value)
builder += " $value"
builder += "\" \\\n "
}
if (body.isNotEmpty())
builder += "-d '" + String(body) + "' \\\n "
builder += "\"" + connection.url + "\""
return builder
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment