https://kotlinlang.org logo
#http4k
Title
# http4k
r

Razi Kheir

12/04/2019, 1:49 AM
Hey @dave For some reason this snippet of code is not compiling for me, it fails the += operator. I am using it from: https://www.http4k.org/cookbook/typesafe_http_contracts/
Copy code
"Unresolved reference. None of the following candidates is applicable because of receiver type mismatch"
I wonder if the issue is me injecting also other things into the handler that returns an HttpHandler
Attempted API: Get a random generated test:
Copy code
api/v1/test
Get a specific test:
Copy code
api/v1/tests/123132
I resolved it by doing this:
Copy code
"/test" / Path.string().of("testId", "A Specific Test") meta {} bindContract Method.GET to handler(injectedService)
Copy code
handler(injectedService: InjectedService) = {
        test: String? -> { _: Request ->  internalHandler(injectedService).invoke(test)
}}
I wonder if this is the right way to do this… Also now I get this route only for
Copy code
api/v1/test/{testId}
But get 404 on
Copy code
api/v1/test/
d

dave

12/04/2019, 6:22 AM
That is the right way to do it - the compile error will happen if you bind to a function that doesn't take the string (which doesn't need the ? Btw).
As for the match, the 404 will probably be because it's trying to match a handler without the path Param (will need to check)
r

Razi Kheir

12/04/2019, 6:26 AM
So if there is a parameter then inherently it’s a required parameter? (not optional)
d

dave

12/04/2019, 6:34 AM
That would follow yes. Afk at the mo, but if you add another route without the Param and it matches then it will be the route matching algorithm in concert with the behavuour of getting path params from the actual underlying Uri object
👍 1
r

Razi Kheir

12/04/2019, 8:37 AM
Yup, seems it works if I add it as a separate path.
👍 1
Is this an issue? Should it be possible to have an optional trailing parameter and passing it as nullable? It’s weird to make two separate paths/handlers for same path with and without a trailing parameter.
By the way i’m working with contracts and bindContract