Ktor: REST API for Mobile | raywenderlich.com

In this tutorial, you’ll create a REST API server for mobile apps using the new Ktor framework from JetBrains.

This is a companion discussion topic for the original entry at https://www.raywenderlich.com/7265034-ktor-rest-api-for-mobile
1 Like

Hello. Amazing tutorial. This is like the best content available on the whole on Internet about ‘Ktor’ I guess. But, I have found a small issue while I was setting up the environment variables after copying it from the article.


The JDBC_DATABASE_URL is set as, jdbc:postgresql:todos?user=postgres; As you can see the user is set as “postgres;” with an extra semi colon. I was getting the error, ‘fatal: no user ‘postgres;’ was found’. I was debugging the issue from last night. For a postgres novice like me it was a little bit difficult to finally find the issue. Hope you will update the article soon.

Nevertheless, this was an amazing article. Now going to create my own REST APIs. Thank you so much again.

@clintpaul89 Thanks very much for your post. Were you able to get your code to work after you removed the semicolon, or were you still having issues? Just wondering.

1 Like

Yes. It worked like a charm after I removed the semicolon. :grinning:

1 Like

@clintpaul89 Good to hear!

The best tutorial for Ktor I’ve ever seen. A big thanks to the author! :hearts:

I am getting this error:

org.postgresql.util.PSQLException: The server requested password-based authentication, but no password was provided.

What should I do?

You should change pg_hba.conf as written here:

and add this code:
config.password = “your-pass”
to hikari()

Thanks for the great tutorial. It is one of those best tutorials I’ve seen.

There is a bug/error in the solution code (calling this function will delete all the todo entries):
override suspend fun deleteTodo(userId: Int, todoId: Int) {
dbQuery {
Todos.deleteWhere {

it should be using org.jetbrains.exposed.sql.and
Todos.deleteWhere {
Todos.id.eq(todoId) and Todos.userId.eq(userId)

@kevindmoore Can you please help with this when you get a chance? Thank you - much appreciated! :]

@dwalk Thanks for the catch. I’ve updated the downloadable projects

I am getting this error with Postman following the code, I’ve double checked everything. Any tips as how I can debug this: ERROR Application - Unhandled: POST - /v1/users/create

Are you sure that you’ve created this route? Make sure it is hooked up

1 Like

I think I had an incorrect header param, which then fixed the issue. However, I am getting one more bug now, I am able to add a user to the data base, but the Route.users still throws a 500 error because on this line call.sessions.set(MySession(userId)) I get this exception, I can’t seem to wrangle it:

java.lang.IllegalArgumentException: Session data for type class com.raywenderlich.Authentication.MySession was not registered

Great tutorial! I’m trying to write unit tests for the application. For example:

    fun test_createAccount_correctPath_post_success() {
        testApp {
            coEvery { repository.addUser(any(), any(), any()) } returns user

            handleRequest(HttpMethod.Post, "/v1/users/create") {
                addHeader(HttpHeaders.ContentType, ContentType.Application.FormUrlEncoded.toString())
                setBody(listOf("email" to email, "displayName" to displayName, "password" to password).formUrlEncode())
            }.apply {
                assertEquals(HttpStatusCode.Created, response.status())
                assertEquals("New user created", response.content)

But the mocking of the suspend function addUser() doesn’t seem to work - the database still tries to find the user and as it already exists it returns an error. Any ideas what I’m doing wrong? Thank you!

I would have to see more of your code. How are you passing the mocked repository to the server?

I downloaded the final code and I’m getting this error even after updating pgjdbc drivers and using jdk 16:
Caused by: com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initialize pool: SCRAM authentication is not supported by this driver. You need JDK >= 8 and pgjdbc >= 42.2.0 (not “.jre” versions)

Do you have the Java SDK with a version at least 8 with paths setup to that installation?

Yes, i have java sdk 16. And I think the paths are already setup to that installation?

@kevindmoore , Im trying to follow this tutorial but, unfortunately, got errors when dealing with routing

Route’ can’t be called in this context by implicit receiver. Use the explicit one if necessary

Using ur code - doesn’t compile, fails on post<>, etc in UserRoute and TodosRoute