Skip to content

Instantly share code, notes, and snippets.

@mertcb
Last active August 15, 2020 21:02
Show Gist options
  • Save mertcb/650c6c64de27b2b27c6bbc8c9873b525 to your computer and use it in GitHub Desktop.
Save mertcb/650c6c64de27b2b27c6bbc8c9873b525 to your computer and use it in GitHub Desktop.
package com.mertcb.sskotlin
import io.ktor.application.*
import io.ktor.features.ContentNegotiation
import io.ktor.gson.gson
import io.ktor.http.*
import io.ktor.response.*
import io.ktor.request.*
import io.ktor.routing.*
import java.text.DateFormat
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) {
// intalling and configuring GSON
install(ContentNegotiation) {
gson {
setDateFormat(DateFormat.LONG)
setPrettyPrinting()
}
}
routing {
get("/"){
call.respondText("Hello, Kotlin!", ContentType.Application.Json)
}
// our post request with endpoint "greet"
post("/greet") {
val postVal: User= call.receive<User>()
call.respondText("Hello, ${postVal.name}")
}
}
}
// our data class to encode given JSON
data class User(val name:String)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment