Having trouble updating to new Ktor dependencies, ...
# ktor
s
Having trouble updating to new Ktor dependencies, getting close by importing from plugins instead of features, but still having trouble installing contentNegotiation and json serialization, even following docs didn't seem to work as described there, :(
t
If you mean Ktor 2.0 beta, this is what I did:
Copy code
private val client = HttpClient {
            install(ContentNegotiation) {
                json(Json { ignoreUnknownKeys = true; isLenient = true; useAlternativeNames = false; })
            }
        }
And use setBody instead of body =
💯 1
a
Here is an equivalent code for Ktor 2.0.0:
Copy code
import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO
import io.ktor.client.plugins.*
import io.ktor.client.request.*
import io.ktor.http.ContentType
import io.ktor.serialization.kotlinx.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.serialization.Serializable

object SlackBotApiService {
    private val client = HttpClient(CIO) {
        install(ContentNegotiation) {
            register(ContentType.Application.Json, KotlinxSerializationConverter(DefaultJson))
        }
        defaultRequest {
            header("Content-Type", "application/json")
        }
    }
    @Serializable
    data class Message(val text: String, val type: String = "mrkdwn")

    suspend fun makePost(messageText: String) {
        <http://client.post|client.post> {
            url("<https://hooks.slack.com/services/************>")
            setBody(Message(text = messageText))
        }
    }
}
🙏 1
🙌 1