I am unable to setup a contractroute with a path v...
# http4k
n
I am unable to setup a contractroute with a path variable ... here is the code: val vendor = Path.of("vendorType") return "/api/v0.0.1/{vendorType}/health-check" meta { summary = "Check the availability of the vendor service" description = "Send a dummy request to the vendor service and reports its status" } bindContract GET to { request -> healthCheckController(vendor(request)) } calling the route with GET {baseurl}/api/v0.0.1/SOME_VENDOR/health-check I get a 404 If I print the routes I get this: "description" : "/GET: /api/v0.0.1/{{vendorType}}/health-check/api/v0.0.1/openapi.json/api/v0.0.1/{{vendorType}}/health-check", that part with "openapi.json" is quite strange but it's present even for other (working) routes .. don't know how to fix this ...
s
You need to break up your path definition. Here's an example:
Copy code
import org.http4k.contract.contract
import org.http4k.contract.div
import org.http4k.contract.meta
import org.http4k.core.Method.GET
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status.Companion.OK
import org.http4k.lens.Path

fun main() {
    val endpoint = "/api/v0.0.1" / Path.of("vendorType") / "healthcheck" meta {
        summary = "xyz"
    } bindContract GET to { _, vendor ->
        { _: Request ->
            Response(OK).body("hello $vendor")
        }
    }

    val app = contract { routes += endpoint }

    app(Request(GET, "api/v0.0.1/acme/healthcheck")).also(::println) //returns 200
}
n
Ookk I see thank you very much
Hi it still doesn't work to me ... don't understand where to look at .. I tried to mimic your example, but I get 400
Copy code
val endpoint = "/api/v0.0.1" / Path.of("vendorType") / "health-check" meta {
    summary = "Check the availability of the vendor service"
    description = "Send a dummy request to the vendor service and reports its status"
} bindContract GET to { _, vendor ->
    { _ ->
        healthCheckController(vendor, mavServiceClient)
    }
}

val app = contract { routes += endpoint }
app(org.http4k.core.Request(GET, "/api/v0.0.1/acme/health-check")).also(::println) // Get 400 and message "Vendor type specified health-check is not valid"
ah the dash, could be it the problem?
no it isn't and, btw, it's allowed in url
s
The problem must be in
healthCheckController
because the routing above works. You can try it by just returning
Response(OK).body("hello $vendor")
as a test.
n
Ok thank you very much
It works but it prints, as vendor, the wrong segment, I see "health-check" instead of "acme"
Copy code
val endpoint = "/$apiPrefix" / Path.of("vendorType") / "health-check" meta {
    summary = "Check the availability of the vendor service"
    description = "Send a dummy request to the vendor service and reports its status"
} bindContract GET to { _, vendor ->
    { _ ->
        Response(Status.OK).body("Hello $vendor") // healthCheckController(vendor, mavServiceClient)
    }
}

val app = contract { routes += endpoint }
DebuggingFilters.PrintRequestAndResponse().then(app)(org.http4k.core.Request(GET, "/api/v0.0.1/acme/health-check")).also(::println)
Outputs: ***** REQUEST: GET: /api/v0.0.1/acme/health-check ***** GET /api/v0.0.1/acme/health-check HTTP/1.1 ***** RESPONSE 200 to GET: /api/v0.0.1/acme/health-check ***** HTTP/1.1 200 OK Hello health-check HTTP/1.1 200 OK Hello health-check > Task :test
s
The vendor is the other parameter. Sorry (just throwing code in a playground). It should be `
Copy code
} bindContract GET to { vendor, _ ->
n
ah ok sorry I could have seen myself
it works, thanks a lot, as always, for your time, http4k support is great
❤️ 1
s
You're welcome 🙂