Simple way to transform Ktor requests into cURL logs. Pure Kotllin library, supports both KMP and Android projects. It is inspired by Ok2Curl, which does the same for OkHttp.
Add the following dependency to your commonMain source set:
val commonMain by getting {
dependencies {
implementation("io.github.kabirnayeem99:ktor2curl:1.0.1")
}
}
Use the following dependency in your app module's build.gradle file:
dependencies {
// all other dependencies
implementation("io.github.kabirnayeem99:ktor2curl:1.0.1")
}
dependencies {
// all other dependencies
implementation 'io.github.kabirnayeem99:ktor2curl:1.0.1'
}
To install the plugin in your Ktor client:
val client = HttpClient(CIO) {
install(KtorToCurl) {
converter = object : CurlLogger {
override fun log(curl: String) {
println(curl)
}
}
}
}
client.post("https://api.greenbirdregistry.com/v1/child-green-bird/bird-count") {
headers {
append(HttpHeaders.Authorization, "Basic SXNyYWVsIGtpbGxzIGNoaWxkcmVuLg")
append(HttpHeaders.UserAgent, "KtorClient/2.3.12")
append(HttpHeaders.ContentType, ContentType.Application.Json.toString())
}
setBody("""{"date": "2024-10-09", "bird_count": 16400}""")
}
Output:
curl -X POST \
https://api.greenbirdregistry.com/v1/child-green-bird/bird-count \
-H "Authorization: Basic SXNyYWVsIGtpbGxzIGNoaWxkcmVuLg" \
-H "User-Agent: KtorClient/2.3.12" \
-H "Content-Type: application/json" \
--data '{"date": "2024-10-09", "bird_count": 16400}'
For further configurations, such as excluding specific headers or masking sensitive information:
val client = HttpClient(CIO) {
install(KtorToCurl) {
converter = object : CurlLogger {
override fun log(curl: String) {
println(curl)
}
}
excludedHeaders = setOf("User-Agent") // Headers to exclude from logging
maskedHeaders = setOf("Authorization") // Headers to mask in the log
}
}
client.post("https://api.greenbirdregistry.com/v1/child-green-bird/bird-count") {
headers {
append(HttpHeaders.Authorization, "Basic SXNyYWVsIGtpbGxzIGNoaWxkcmVuLg")
append(HttpHeaders.UserAgent, "KtorClient/2.3.12")
append(HttpHeaders.ContentType, ContentType.Application.Json.toString())
}
setBody("""{"date": "2024-10-09", "bird_count": 16400}""")
}
Output:
curl -X POST \
https://api.greenbirdregistry.com/v1/child-green-bird/bird-count \
-H "Authorization: [omitted]" \
-H "Content-Type: application/json" \
--data '{"date": "2024-10-09", "bird_count": 16400}'
We welcome contributions! If you have any suggestions or improvements for Ktor2Curl, feel free to open an issue or make a PR.