Created
September 25, 2025 13:22
-
-
Save nsmirosh/823542a38ce2457faae9e6eaf65df2f4 to your computer and use it in GitHub Desktop.
PrettyPrint with Ktor
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 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