Ktor: Send and Receive Json

Max Großmann
3 min readSep 18, 2020

In this article you will learn how to create a post route which receives json, does some logic with the content of the json and returns json content to the executor.

Prerequirements

You should know how to create a basic ktor rest application and know how to test your routes, e.g. via curl command.

If don’t do so, consider reading my first post about ktor:

What we want to build

Let’s build a small login application. The user sends a username and password to the ktor backend. The server responds whether to credentials are correct.

For simplicity reasons we check the credentials against hardcoded strings. For real applications there might be a database behind the api which holds all valid users.

Start coding

Install Ktor-Gson Library

Open the build.gradle file and add the following line to the dependencies block.

implementation "io.ktor:ktor-gson:$ktor_version"

Create Request and Response Classes

Ktor works which typesafe objects instead of raw json strings and does the convertion for us.

So let’s create a entities Folder and create two data classes to that folder. You can create two sperarated files or put both classes in one file.

data class LoginRequest(
val username: String,
val password: String
)
data class LoginResponse(
val ok: Boolean,
val message: String
)

Install ContentNegotiotiation Feature

Go to the module declaration (probably Application.kt) and install the Content-Negotiotiation Feature to this module. The Content-Negotiation Feature provides the convertion logic between data objects and text content, in our case json.

install(ContentNegotiation) {
gson {
setPrettyPrinting()
}
}

Write POST Route

Now we are done with preparing our environment. Let’s create the login-Route which receives a LoginRequest Object, checks whether the credentials are correct and returns a LoginResponse Object.

Add the following post-block to the routing-Block:

post("/login") {
val loginRequest = call.receive<LoginRequest>()

if (loginRequest.username == "admin"
&& loginRequest.password == "adminpw") {
call.respond(
LoginResponse(true, "Login successful!")
)
} else {
call.respond(
LoginResponse(false, "Invalid password!")
)
}
}

Run and Test

So we are finally done. Let’s run the main-Method and test whether our application works.

Curl Command for Windows:

curl -X POST --header "Content-Type: application/json" --data {"username":"admin","password":"adminpw"} http://localhost:8081/login

Curl Command for Linux:

curl -X POST --header "Content-Type: application/json" --data '{"username":"admin","password":"adminpw"}' http://localhost:8081/login
  • Note, that the port can be different in your environment, check your console
  • For Linux the json needs to wrapped in single quotation marks

Reponse:

“Login successful“ after curl

When we change the password in the curl command, we get:

“Invalid password” after curl

I hope you liked that part of my ktor series. If you enjoyed that part, please leave some clapps. :)

Also check out my Youtube Channel for some other tech tutorials!

--

--