Kotlin/Native and Multiplatform 路 Ktor and Coroutines | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/1304635-kotlin-native-and-multiplatform/lessons/19

Thanks for this tutorial I found it very useful.

Could you please give me an example how to send a HttpClient POST request where you post object?
I try many things but with no luck. I get 400 http error code or error which says that my object is in incorrect format.

Thank you for the question @grzehotnik! Here is an example from the Ktor docs:

data class HelloWorld(val hello: String)

val client = HttpClient(Apache) {
    install(JsonFeature) {
        serializer = GsonSerializer {
            // Configurable .GsonBuilder
            serializeNulls()
            disableHtmlEscaping()
        }
    }
}

client.post<Unit> {
    url(URL("http://127.0.0.1:8080/"))
    body = HelloWorld(hello = "world")
}

In this case, you are sending a HelloWorld object in the POST request body.

Let me know if that helps or if you need more info. Thanks again!

Thanks for quick response.

What dependencies I need for Apache (what if I use IIS instead?), GsonSerializer, serializeNulls, serializer, JsonFeature. Compiler gives me unresolved references for those.

Thank you

Thanks @grzehotnik! Your best bet for diagnosing the issue is to try to find a similar example in the Ktor samples, available here:

Hopefully your use case is covered in a sample with the same dependencies. Thanks again!

If other folks are having an issue when trying the Gradle build, something like
鈥otlin native The abi versions don鈥檛 match. Expected 鈥榌8]鈥, found 鈥5鈥欌

Make sure that you are using compatible kotlin and ktor library versions. I was using kotlin 1.3.31 and was having that issue, you can either use the same library versions as in the tutorial (kotlin 1.3.21 and ktor 1.1.2) or use the latest versions of both libraries (kotlin 1.3.31 and ktor 1.1.5 as of today).

P.D. I am taking advantage of the free weekend!! :sunglasses:

@cryptoseven Thank you for the heads up - much appreciated!