Skip to content

Instantly share code, notes, and snippets.

@nsmirosh
Created September 25, 2025 13:22
Show Gist options
  • Select an option

  • Save nsmirosh/823542a38ce2457faae9e6eaf65df2f4 to your computer and use it in GitHub Desktop.

Select an option

Save nsmirosh/823542a38ce2457faae9e6eaf65df2f4 to your computer and use it in GitHub Desktop.
PrettyPrint with Ktor
fun buildHttpClient() = HttpClient(Okhttp){
val formatter = Json {
prettyPrint = true
isLenient = true
ignoreUnknownKeys = true
coerceInputValues = true
}
install(ContentNegotiation) {
json(formatter)
}
install(Logging) {
logger = Logger.SIMPLE
level = LogLevel.HEADERS
}
HttpResponseValidator {
validateResponse { response ->
val text = response.bodyAsText()
if (text.startsWith("{")) {
println(
formatter.encodeToString(
JsonElement.serializer(),
Json.parseToJsonElement(text)
)
)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment