Hi can anybody help me out with this. I have a KMM...
# ktor
f
Hi can anybody help me out with this. I have a KMM ktor client connected with a ktor server/backend. I can authenticate and get a token but when I call this get from client:
Copy code
suspend fun courses(request: CourseRequest): CourseResponse = client.get {
        url("courses")
        parameter("courseLevel", request.courseLevel)
        headers {
            append(HttpHeaders.Authorization, sharedSettingsHelper.token)
        }
    }.body()
I get this back from log
Copy code
2023-08-31 10:55:17.808 23243-23504 System.out              com.mayka.talamyd                    I  HttpClient: REQUEST: <http://192.168.2.3:8080/courses?courseLevel=5>
2023-08-31 10:55:17.808 23243-23504 System.out              com.mayka.talamyd                    I  METHOD: HttpMethod(value=GET)
2023-08-31 10:55:17.808 23243-23504 System.out              com.mayka.talamyd                    I  COMMON HEADERS
2023-08-31 10:55:17.808 23243-23504 System.out              com.mayka.talamyd                    I  -> Accept: application/json; application/json
2023-08-31 10:55:17.808 23243-23504 System.out              com.mayka.talamyd                    I  -> Accept-Charset: UTF-8
2023-08-31 10:55:17.808 23243-23504 System.out              com.mayka.talamyd                    I  -> Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJ1c2VycyIsImlzcyI6InRhbGFteWQuY29tIiwiZW1haWwiOiJmYXJpZEBleGFtcGxlLmNvbSIsImV4cCI6MTY5MzQ3MjE3Nn0.ddXteY1ZU0YzT8I2CVesKmKDKls-hxZ9bCxOYfcBXDo
2023-08-31 10:55:17.808 23243-23504 System.out              com.mayka.talamyd                    I  -> Content-Type: application/json
2023-08-31 10:55:17.808 23243-23504 System.out              com.mayka.talamyd                    I  CONTENT HEADERS
2023-08-31 10:55:17.808 23243-23504 System.out              com.mayka.talamyd                    I  -> Content-Length: 0
2023-08-31 10:55:17.808 23243-23504 System.out              com.mayka.talamyd                    I  BODY Content-Type: null
2023-08-31 10:55:17.808 23243-23504 System.out              com.mayka.talamyd                    I  BODY START
2023-08-31 10:55:17.808 23243-23504 System.out              com.mayka.talamyd                    I  
2023-08-31 10:55:17.808 23243-23504 System.out              com.mayka.talamyd                    I  BODY END
2023-08-31 10:55:17.827 23243-23508 System.out              com.mayka.talamyd                    I  HttpClient: RESPONSE: 404 Not Found
2023-08-31 10:55:17.827 23243-23508 System.out              com.mayka.talamyd                    I  METHOD: HttpMethod(value=GET)
2023-08-31 10:55:17.827 23243-23508 System.out              com.mayka.talamyd                    I  FROM: <http://192.168.2.3:8080/courses?courseLevel=5>
2023-08-31 10:55:17.827 23243-23508 System.out              com.mayka.talamyd                    I  COMMON HEADERS
2023-08-31 10:55:17.827 23243-23508 System.out              com.mayka.talamyd                    I  -> connection: keep-alive
2023-08-31 10:55:17.827 23243-23508 System.out              com.mayka.talamyd                    I  -> content-length: 0
2023-08-31 10:55:17.827 23243-23508 System.out              com.mayka.talamyd                    I  -> x-android-received-millis: 1693472117823
2023-08-31 10:55:17.827 23243-23508 System.out              com.mayka.talamyd                    I  -> x-android-response-source: NETWORK 404
2023-08-31 10:55:17.827 23243-23508 System.out              com.mayka.talamyd                    I  -> x-android-selected-protocol: http/1.1
2023-08-31 10:55:17.827 23243-23508 System.out              com.mayka.talamyd                    I  -> x-android-sent-millis: 1693472117810
2023-08-31 10:55:17.827 23243-23508 System.out              com.mayka.talamyd                    I  BODY Content-Type: null
2023-08-31 10:55:17.827 23243-23508 System.out              com.mayka.talamyd                    I  BODY START
This is my code from serverside
Copy code
authenticate {
        route("courses"){
            get("{courseLevel}") {
                val params = call.parameters["courseLevel"]
                println("__PARAMS = $params")

                if (params == null) {
                    call.respond(
                        status = HttpStatusCode.BadRequest, message = AuthResponse(
                            errorMessage = "Error getting courses!"
                        )
                    )

                    return@get
                }

                val result = repository.getCourses(params.toInt())
                call.respond(
                    status = result.code, message = result.data
                )
            }
        }
    }
Copy code
fun Application.configureRouting() {
    intercept(ApplicationCallPipeline.Fallback) {
        if (call.isHandled) return@intercept
        val status = call.response.status() ?: HttpStatusCode.NotFound
        call.respond(status)
    }
    routing {
        authRouting()
        courseRouting()
    }
}
Any help would be much appreciated.
a
Can you please state the problem?
f
I dont really understand why i get a 404 response
a
Do you get 404 without
authenticate
and
get("{courseLevel}")
routes?
f
No then it works
Also when I use
authenticate{}
without the parameter in the
get
it works
Nobody can help with this issue?
It looks like
call.parameters
works like
call.request.queryParameters
So instead of
Copy code
get("/user/{courseLevel}") {
    val params = call.parameters["courseLevel"] 
        // ...
    
}
You need to use
Copy code
get("/user") {. ---> without /{courseLevel}
   val params = call.parameters["courseLevel"] 
        // ...
    
}
a
With the
get("{courseLevel}")
route you need to make a request to like http://192.168.2.3:8080/courses/5 to get 200 response.