Unable to use kotlinx.serialization with ktor client

36 views Asked by At

I have installed the recommended plugins and I attach the class files I have made. When I use them to send a post request, I get the below error

java.lang.IllegalStateException: Fail to prepare request body for sending.
The body type is: class data.model.dto.RegisterDto (Kotlin reflection is not available), with Content-Type: application/json. If you expect serialized body, please check that you have installed the corresponding plugin(like ContentNegotiation) and set Content-Type header.
at io.ktor.client.plugins.HttpSend$Plugin$install$1.invokeSuspend(HttpSend.kt:88)

HttpClient(CIO) {
    install(ContentNegotiation) {
        json(Json {
            prettyPrint = true
            isLenient = true
            ignoreUnknownKeys = true
        })
    }
}

@Serializable
data class RegisterDto(
    val email: String,
    val password: String
)

ApiClient.httpClient.post {
    url("http:////192.168.0.104:8090/auth/login")
    contentType(ContentType.Application.Json)
    body = RegisterDto("[email protected]", "1234567890")
}
1

There are 1 answers

0
jagadheshvar On

Resolved, had to use setBody(RegisterDto("[email protected]", "1234567890") instead of body = RegisterDto("[email protected]", "1234567890")