Hello :wave: While using Ktor HttpClient Is there ...
# ktor
d
Hello đź‘‹ While using Ktor HttpClient Is there a way to automatically set
Content-Length
header (default in v1.6) vs
Transfer-Encoding: chunked
(default in v2.0)?
v1.6 code
Copy code
HttpClient(engineFactory = Apache) {
    install(HttpTimeout) {
        connectTimeoutMillis = connectTimeout
        requestTimeoutMillis = readTimeout
    }
    install(feature = JsonFeature)
}.use { client ->
    runBlocking {
        val introspectionResult = try {
            <http://client.post|client.post><Map<String, Any?>> {
                url(endpoint)
                contentType(ContentType.Application.Json)
                httpHeaders.forEach { (name, value) ->
                    header(name, value)
                }
                body = mapOf(
                    "query" to INTROSPECTION_QUERY,
                    "operationName" to "IntrospectionQuery"
                )
            }
        } catch (e: Throwable) {
            when (e) {
                is ClientRequestException, is HttpRequestTimeoutException, is UnknownHostException -> throw e
                else -> throw RuntimeException("Unable to run introspection query against the specified endpoint=$endpoint", e)
            }
        }
}
v2.0 code
Copy code
HttpClient(engineFactory = Apache) {
    install(HttpTimeout) {
        connectTimeoutMillis = connectTimeout
        requestTimeoutMillis = readTimeout
    }
    install(ContentNegotiation) {
        jackson()
    }
}.use { client ->
    runBlocking {
        val introspectionResult = try {
            <http://client.post|client.post> {
                url(endpoint)
                contentType(ContentType.Application.Json)
                httpHeaders.forEach { (name, value) ->
                    header(name, value)
                }
                setBody(
                    mapOf(
                        "query" to INTROSPECTION_QUERY,
                        "operationName" to "IntrospectionQuery"
                    )
                )
                expectSuccess = true
            }.body<Map<String, Any?>>()
        } catch (e: Throwable) {
            when (e) {
                is ClientRequestException, is HttpRequestTimeoutException, is UnknownHostException -> throw e
                else -> throw RuntimeException("Unable to run introspection query against the specified endpoint=$endpoint", e)
            }
        }
}
r
3 options: 1. use
KotlinxSerializationConverter
2. create your own
JacksonConverter
, that will return
TextContent
instead of
OutputStreamContent
. The converter itself is about 100 loc and you can copy most of it from existing one 3. create a ticket in YouTrack and wait when the Ktor team will address it
d
thanks, cannot use kotlinx as the resulting data is just arbitrary map and would rather not create custom converter. I'll create a ticket. Thanks again!
r
isn’t kotlinx.serialization supports deserialization to map?
d
from my past experience its not as simple as with jackson (had to use some custom serializers in the past)