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

Sam Michael

12/28/2021, 3:47 PM
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

tylerwilson

12/28/2021, 4:50 PM
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

Aleksei Tirman [JB]

12/29/2021, 8:06 AM
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
3 Views