Home > database >  ktor Apache client post request - No transformation found: class kotlinx.coroutines.io.ByteBufferCha
ktor Apache client post request - No transformation found: class kotlinx.coroutines.io.ByteBufferCha

Time:09-06

I have to use very old ktor version (1.2.6) - please don't tell me to upgrade I can't do it now. I am trying to send a post request to another service which I mocked for testing with wiremock.
This is my client config:

object HTTP {
    val client = HttpClient(Apache) {
        followRedirects = false
        engine {
            customizeClient {
                setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
            }
        }

        install(JsonFeature) {
            serializer = JacksonSerializer()
        }
    }
}

And this is code that actually tries to do post:

val url = "http://localhost:9099/v0/test"
val response = HTTP.client.post<TestResponse>(url) {
        header(HttpHeaders.ContentType, "application/json")
        contentType(ContentType.Application.Json)
        body = TestRequest()
}

and this is my wiremock configuration:

WireMock.stubFor(
  WireMock.post(WireMock.urlMatching("/v0/test"))
     .willReturn(
        WireMock.aResponse().withStatus(200)
           .withBody(jacksonObjectMapper().writeValueAsString(testResponse))
     )
)

When I hit this wiremock with curl it works fine, but calling it with the code above results in:

No transformation found: class kotlinx.coroutines.io.ByteBufferChannel -> class com.test.TestResponse
io.ktor.client.call.NoTransformationFoundException: No transformation found: class kotlinx.coroutines.io.ByteBufferChannel -> class com.test.TestResponse
    at io.ktor.client.call.HttpClientCall.receive(HttpClientCall.kt:88)

Can someone please help ?

CodePudding user response:

To solve your problem add the Content-Type: application/json header to the response:

stubFor(
    post(urlMatching("/v0/test"))
        .willReturn(
            aResponse().withStatus(200)
                .withHeader("Content-Type", "application/json")
                .withBody(jacksonObjectMapper().writeValueAsString(TestResponse(123)))
        )
)
  • Related