Hello, How can I get the http body message in my r...
# ktor
n
Hello, How can I get the http body message in my response when i get a http error ? I see the response.readText() but it doesn't seem to work...
g
I'm in the middle of doing a migration from 1.2.3 -> 1.4 and see this
Copy code
val response = <http://httpClient.post|httpClient.post><HttpResponse>("$localServerUrl/searches") {
                body = TextContent(request.toJsonString(), ContentType.Application.Json)
                accept(ContentType.Application.Json)
            }

response.readText() 

// throws io.ktor.client.call.NoTransformationFoundException: No transformation found: class kotlinx.coroutines.io.ByteBufferChannel -> class io.ktor.utils.io.core.Input
is that the same thing you are experiencing?
m
try {} catch and read the exception message
you can also add: expectSuccess = false to you client config
n
I found a solution with
Copy code
val message = httpResponse.content.readText(
    httpResponse.contentType()?.charset() ?: Charsets.UTF_8)

private suspend inline fun ByteReadChannel.readText(charset: Charset): String =
    readRemaining().readText(charset = charset)
g
@mserralta the expectSuccess false is already in place in my implementation 🤔
m
@gotoOla in your case i think it is related to the response content type you are receiving, can u confirm you response type is application/json and not something else?