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

Tobias Gronbach

02/19/2022, 12:14 PM
Still have some error I get after migrating to version 2.0.0-beta-1 that I do not understand (Ktor-Client). If I try to execute the following code:
Copy code
override suspend fun authenticateUser(userNameHash: String, passwordHash: String): UserAuthData? {
        return try {
            <http://client.post|client.post>(HttpRoutes.AUTHENTICATE_USER_FQDN) {
                contentType(ContentType.Application.Json)
                setBody {
                    UserRegistrationData(
                        userNameHash = userNameHash,
                        passwordHash = passwordHash
                    )
                }
            }.body<UserAuthData>()
        } catch (e: Exception) {
 ...
           }
    }
Data Class looks like this:
Copy code
@kotlinx.serialization.Serializable
data class UserRegistrationData(
    val userNameHash: String,
    val passwordHash: String
)
HttpClient
Copy code
HttpClient(Android) {
....
        install(ContentNegotiation) {
                json(Json {
                    ignoreUnknownKeys = true
                    isLenient = true
                }
                )
            }
....
}
Dependencies (for serialization)
Copy code
implementation("io.ktor:ktor-client-content-negotiation:$ktor_version")
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktor_version")
Error Message: Line: 58, Method: authenticateUser: Serializer for class 'null' is not found. Mark the class as @Serializable or provide the serializer explicitly.
s

Sam

02/19/2022, 12:22 PM
Is
setBody
supposed to be taking a lambda argument?
I would have expected
Copy code
setBody(UserRegistrationData(
    userNameHash = userNameHash,
    passwordHash = passwordHash
))
Probably it's trying to serialize the lambda function, hence the error
t

Tobias Gronbach

02/19/2022, 12:48 PM
Thanks Sam! That solved my issue! I appreciate your time and help!! 👌😀
blob smile 1
11 Views