THEHORIZIAN

Ktor for Mobile Developers: Is it Easy to Learn Server Side Coding? Part-2

Backend for mobile engineers with Kotlin and Ktor

implementation "io.ktor:ktor-jackson:$ktor_version"
install(ContentNegotiation) {
jackson {
enable(SerializationFeature.INDENT_OUTPUT)
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
data class User(
@JsonProperty("id") val id: UUID = UUID.randomUUID(),
@JsonProperty("email") val email: String="myemail@gmail.com",
@JsonProperty("first_name") val firstName: String="FirstName",
@JsonProperty("last_name") val lastName: String="LastName",
@JsonProperty("profile_image_url") val profileImageUrl: String?=null
)
routing {
get("/") {
call.respond(User())
}
/*---------------------*/
post { }
put { }
delete { }
}
{
"id" : "c4d93580-b719-41b5-bb05-be480b04a493",
"email" : "myemail@gmail.com",
"first_name" : "FirstName",
"last_name" : "LastName",
"profile_image_url" : null
}
routing {
get("/") {
call.respond(User())
}

post("") {
val userDto = call.receive<User>()
if ("FirstName" == userDto.firstName){
call.respondText { "User Matched Successfully." }
}else{
call.respondText { "User does not match." }
}
}

/*---------------------*/
put { }
delete { }
}
Screenshot of POST web request in POSTMAN

Android Developer | Kotlin Developer | Open-Source Enthusiast | Part-time Blogger | 💻 🚘 🎸 enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store