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.

JDBC_DRIVER=org.postgresql.Driver
JDBC_DATABASE_URL=jdbc:postgresql:todos?user=postgres;
SECRET_KEY=898748674728934843
JWT_SECRET=898748674728934843

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 {
Todos.id.eq(todoId)
Todos.userId.eq(userId)
}
}
}

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:

  @Test
    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