https://kotlinlang.org logo
#ktor
Title
# ktor
n

Nicolas Bourdin

08/27/2020, 9:37 AM
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

gotoOla

08/28/2020, 8:54 AM
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

mserralta

08/28/2020, 12:00 PM
try {} catch and read the exception message
you can also add: expectSuccess = false to you client config
n

Nicolas Bourdin

08/28/2020, 1:07 PM
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

gotoOla

08/28/2020, 1:48 PM
@mserralta the expectSuccess false is already in place in my implementation 🤔
m

mserralta

08/31/2020, 8:34 AM
@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?
51 Views