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

hantsy

06/21/2023, 5:05 AM
Hi, we used
ktor-client-cio
to shake hands with an external APIs, but the APIs does not expose a correct ContentType header(
application/octet-stream
) as expected, the content is JSON. I tried to add a default content type to the response via the built-in
HttpSend
plugin like the following, but it did not work.
Copy code
var client = HttpClient(CIO){
            install(ContentNegotiation) {
                register(ContentType.Application.Json, JacksonConverter(objectMapper))
            }
}        
client.plugin(HttpSend).intercept { builder ->
            val originalCall = execute(builder)
//            if (!originalCall.response.headers.contains(HttpHeaders.ContentType) ||
//                originalCall.response.headers[HttpHeaders.ContentType] == ContentType.Application.OctetStream.contentType
//            ) {
                originalCall.response.apply {
                    headers {
                        set(HttpHeaders.ContentType, ContentType.Application.Json.contentType)
                    }
                }
           // }
            originalCall
        }
I have tried the Apache 5 engine, it is more flexible for the
engine
config and I configured
customizeClient
Copy code
customizeClient {
                    addResponseInterceptorLast(
                        HttpResponseInterceptor { response, _, _ ->
                            response.setHeader(HttpHeaders.ContentType, ContentType.Application.Json)
                        }
                    )
                }
to add an interceptor there, it worked well. But some of our team members are stick on the CIO engine.
a

Aleksei Tirman [JB]

06/21/2023, 6:16 AM
Why registering a converter for the
application/octet-stream
content type doesn't work for you?
h

hantsy

06/21/2023, 7:42 AM
I want to know what is wrong in usage of
HttpSend
plugin.
treat
application/octet-stream
content with a json converter is ugly.
a

Aleksei Tirman [JB]

06/21/2023, 8:49 AM
You can try this workaround to override the
Content-Type
header.
h

hantsy

06/21/2023, 10:44 AM
We can not modify the response headers via the
HttpSendIntercepter
directly?
a

Aleksei Tirman [JB]

06/21/2023, 11:01 AM
No, because they are immutable there
h

hantsy

06/22/2023, 3:17 AM
If the
HttpSendIntecepter
can not modify request/response, what is the purpose of using it?
And it the response is immutable there, it should not accept a
apply{}
block.
And headers builder has
set
to modify the headers.
a

Aleksei Tirman [JB]

06/22/2023, 7:09 AM
If the
HttpSendIntecepter
can not modify request/response, what is the purpose of using it?
The purpose is to intercept the network calls to implement, for example, redirection, request retrying and so on. You can modify the request through the
HttpRequestBuilder
parameter (the
builder
parameter in your example).
And it the response is immutable there, it should not accept a
apply{}
block.
The following code just returns a new instance of
Headers
.
Copy code
headers {
    set(HttpHeaders.ContentType, ContentType.Application.Json.contentType)
}
In your example, there is no code that tries to modify the response.
2 Views