https://kotlinlang.org logo
#server
Title
n

Nir Golan

11/21/2018, 11:49 AM
hi guys, need some help, first try with ktor, getting the sample template from intellij plugin and with the starter code the app crash.
Copy code
package com.example

import io.ktor.application.*
import io.ktor.response.*
import io.ktor.request.*
import io.ktor.routing.*
import io.ktor.http.*
import io.ktor.gson.*
import io.ktor.features.*
import io.ktor.client.*
import io.ktor.client.features.json.*
import io.ktor.client.request.*
import java.net.URL
import kotlinx.coroutines.*

fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)

@Suppress("unused") // Referenced in application.conf
@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {
    install(ContentNegotiation) {
        gson {
        }
    }

    val client = HttpClient() {
        install(JsonFeature) {
            serializer = GsonSerializer()
        }
    }
    runBlocking {
//         Sample for making a HTTP Client request
//        /*
//        val message = <http://client.post|client.post><JsonSampleClass> {
//            url(URL("<http://127.0.0.1:8080/path/to/endpoint>"))
//            contentType(ContentType.Application.Json)
//            body = JsonSampleClass(hello = "world")
//        }
//        */
    }

    routing {
        get("/") {
            call.respondText("HELLO WORLD!", contentType = ContentType.Text.Plain)
        }

        get("/json/gson") {
            call.respond(mapOf("hello" to "world"))
        }
    }
}

data class JsonSampleClass(val hello: String)
when i'm commenting out the client its working, can any one help? Slack Conversation