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

Elena van Engelen

03/29/2023, 9:53 AM
Hi I am new to ktor , moving from reactive WebClient and having a stupid issue with non compiling code in ktor, does someone have a good example in GitHub for a post Kotlin/JVM + Spring boot 3, e.g.
Copy code
val response = <http://httpClient.post|httpClient.post><MyClass1> {
    url("$myUrl")
    contentType(ContentType.Application.Json)
    body = MyClass2(MyClass3(param1, param2))
}
a

Aleksei Tirman [JB]

03/29/2023, 10:28 AM
Can you share a complete code snippet?
e

Elena van Engelen

03/29/2023, 10:54 AM
Copy code
import io.ktor.client.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import mu.KotlinLogging
import nl.edsn.generated.mar.models.*
import org.springframework.http.*
import org.springframework.stereotype.Service

@Service
class ExampleService(private val httpClient: HttpClient) {

    fun example(){
        val url = "bla"
        val response = <http://httpClient.post|httpClient.post><String> {
            url(url)
            contentType(MediaType.APPLICATION_JSON)
            body("")
        }
    }

    companion object {
        val log = KotlinLogging.logger {}
    }
}
Somehow I cannot get the DSL to compile after post.IntelliJ does not suggest any useful imports , chatGPT was also useless in this case. Its probably something in imports? I have these dependancies:
Copy code
implementation("io.ktor:ktor-client-core:$ktorVersion")
implementation("io.ktor:ktor-client-okhttp:$ktorVersion")
implementation("io.ktor:ktor-client-logging:$ktorVersion")
a

Aleksei Tirman [JB]

03/29/2023, 3:04 PM
What version of Ktor do you use?
If it’s
2.*
, then you need to remove the
String
type parameter and call the
body
method. Please have a look at the migration guide.
e

Elena van Engelen

03/30/2023, 6:39 AM
thank you, that worked!
42 Views