Hi I am new to ktor , moving from reactive WebClie...
# ktor
e
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
Can you share a complete code snippet?
e
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
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
thank you, that worked!