Farid Benhaimoud
08/31/2023, 9:09 AMsuspend 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
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
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
)
}
}
}
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.Aleksei Tirman [JB]
08/31/2023, 10:17 AMFarid Benhaimoud
08/31/2023, 10:30 AMAleksei Tirman [JB]
08/31/2023, 10:31 AMauthenticate
and get("{courseLevel}")
routes?Farid Benhaimoud
08/31/2023, 10:35 AMFarid Benhaimoud
08/31/2023, 10:40 AMauthenticate{}
without the parameter in the get
it worksFarid Benhaimoud
08/31/2023, 6:33 PMFarid Benhaimoud
08/31/2023, 8:17 PMcall.parameters
works like call.request.queryParameters
So instead of
get("/user/{courseLevel}") {
val params = call.parameters["courseLevel"]
// ...
}
You need to use
get("/user") {. ---> without /{courseLevel}
val params = call.parameters["courseLevel"]
// ...
}
Aleksei Tirman [JB]
09/01/2023, 5:14 AMget("{courseLevel}")
route you need to make a request to like http://192.168.2.3:8080/courses/5 to get 200 response.