Shubo
06/11/2025, 8:31 AMWebSocketException
if the response code is not 101.
https://github.com/ktorio/ktor/blob/main/ktor-client/ktor-client-core/common/src/io/ktor/client/plugins/websocket/WebSockets.kt#L202
Is there any way to somehow get the original response from the server?Aleksei Tirman [JB]
06/11/2025, 8:44 AMWebSocketException
is thrown is by intercepting the earlier phase of the response pipeline. Here is an example:
val client = HttpClient(CIO) {
install(WebSockets)
}
client.responsePipeline.intercept(HttpResponsePipeline.Parse) { (info, session) ->
try {
proceed()
} catch (cause: WebSocketException) {
println(context.response)
throw cause
}
}
client.webSocket("<ws://httpbin.org/status/404>") {}
Shubo
06/11/2025, 1:07 PMcontext.response.body()
, it causes a stackoverflow, because of client.responsePipeline.execute(this, subject).response.takeIf { it != NullBody }
being called inside.
Is there a way around it?
I ended up doing context.response.rawContent.run { peek(availableForRead)?.decodeToString() }
, but since rawContent
is an internal api, it'd be better to avoid it.Aleksei Tirman [JB]
06/11/2025, 1:12 PMrawContent
.