Created
February 14, 2022 12:59
-
-
Save meysampg/b40a11dd62d667911da7cf99dd9e7c67 to your computer and use it in GitHub Desktop.
Convert HttpURLConnection to Curl Command in Kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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