Hi. I migrate to 2.x version ktor-client in multip...
# ktor
v
Hi. I migrate to 2.x version ktor-client in multiplatform and have error when send post-request Fail to prepare request body for sending. The body type is: class kotlinx.serialization.json.JsonObject (Kotlin reflection is not available), with Content-Type: null. If you expect serialized body, please check that you have installed the corresponding plugin(like
ContentNegotiation
) and set
Content-Type
header. My config in thread
gradle:
Copy code
sourceSets {
    val commonMain by getting {
        dependencies {
            implementation("com.squareup.sqldelight:runtime:1.5.5")
            implementation("com.squareup.sqldelight:coroutines-extensions:1.5.5")
            implementation("io.ktor:ktor-client-core:2.3.2")
            implementation("io.ktor:ktor-client-logging:2.3.2")
            implementation("io.ktor:ktor-client-content-negotiation:2.3.2")
            implementation("io.ktor:ktor-serialization-kotlinx-json:2.3.2")
            implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1")
            implementation("commons-codec:commons-codec:1.15")
            implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.5.1")
            implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")
client config
Copy code
val httpClient = HttpClient() {
    install(ContentNegotiation) {
        json(
            Json {
                useAlternativeNames = false
                ignoreUnknownKeys = true
                isLenient = true
            }
        )
    }
    install(Logging) {
        logger = Logger.DEFAULT
        level = LogLevel.ALL
    }
    defaultRequest {
        header("Content-Type", "application/json")
request:
Copy code
return try {
    <http://httpClient.post|httpClient.post>(
        db.serverQueries.selectAll().executeAsOneOrNull()?.currentServer + ApiConst.API_PAYOUT
    )
    {
        contentType(ContentType.Application.Json)
        body = Json.encodeToJsonElement(payoutData)
    }.body()
Data class:
Copy code
@Serializable
data class PayoutData(
    var payoutType: String,
    var account: String?,
    var walletType: String,
    var code: Int? = null
)
a
The
body
property does exist only in Ktor 1.. In Ktor 2. the
setBody
method is to send a request body. Can you share the migrated code?
v
Perfect! It work! But I’m not write specially migrate code, only replace method in my network request methods