hantsy
06/21/2023, 5:05 AMktor-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.
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
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.Aleksei Tirman [JB]
06/21/2023, 6:16 AMapplication/octet-stream
content type doesn't work for you?hantsy
06/21/2023, 7:42 AMHttpSend
plugin.application/octet-stream
content with a json converter is ugly.Aleksei Tirman [JB]
06/21/2023, 8:49 AMContent-Type
header.hantsy
06/21/2023, 10:44 AMHttpSendIntercepter
directly?Aleksei Tirman [JB]
06/21/2023, 11:01 AMhantsy
06/22/2023, 3:17 AMHttpSendIntecepter
can not modify request/response, what is the purpose of using it?apply{}
block.set
to modify the headers.Aleksei Tirman [JB]
06/22/2023, 7:09 AMIf theThe purpose is to intercept the network calls to implement, for example, redirection, request retrying and so on. You can modify the request through thecan not modify request/response, what is the purpose of using it?HttpSendIntecepter
HttpRequestBuilder
parameter (the builder
parameter in your example).
And it the response is immutable there, it should not accept aThe following code just returns a new instance ofblock.apply{}
Headers
.
headers {
set(HttpHeaders.ContentType, ContentType.Application.Json.contentType)
}
In your example, there is no code that tries to modify the response.