Hi all, I'm currently trying to implement an SSE ...
# ktor
s
Hi all, I'm currently trying to implement an SSE implemention in Ktor for a Compose Multiplatform client, and more specifically the WASM one using a JS Ktor engine. I'm trying to read out the Cohere Chat Stream API (https://docs.cohere.com/v2/reference/chat-stream) but my request always keeps failing because apparently I'm not allowed to add a request body to an SSE request?
Copy code
Caused by: JsError: Error from javascript[TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.
My implementation looks like this:
Copy code
suspend fun sendCohereChatMessage(msg: String) {
        val chatRequest = ChatRequest(
            stream = true,
            model = "command-r-plus-08-2024",
            messages = listOf(RequestMessage(role = "user", content = msg))
        )

        client.serverSentEvents(
            urlString = "<https://api.cohere.com/v2/chat>",
            request = {
                setBody(chatRequest)
                contentType(ContentType.Application.Json)
            }
        ) {
            incoming.collect { event ->
                println(event)
            }
        }
    }
Does anyone know how to get passed this issue or is it simply not supported?
d
If the error is about a HEAD request, then it seems likely the error is happening elsewhere in your code. Unless there is a bug with the SSE plugin
Oh, I missed it might also be a GET. In your
request
block, make sure you set the method correctly. It should probably be POST
👍 1
s
Oh wow, I totally missed that 🙈! Adding
method = <http://HttpMethod.Post|HttpMethod.Post>
to the request block fixed the issue indeed. Thanks a bunch!
d
Glad that worked.