I have a scenario where I want to return some data...
# http4k
a
I have a scenario where I want to return some data in an
OPTIONS
call. The call works fine with a core http4k router, but if I wrap it in a
contract
, then the contract router never calls my handler and returns an empty 200. Presumably, this was done with the assumption that
OPTIONS
doesn't make much sense in a RESTP API, but that isn't always the case. Is this phenomenon intentional? Is there a way to prevent it?
Copy code
@Test
fun options() {
    val handler = contract {
        routes += "/v1/things" bindContract Method.OPTIONS to { _ ->
            Response(Status.OK).body("lolcats")
        }
    }

    val resp = handler(Request(Method.OPTIONS, "/v1/things"))
    resp shouldHaveStatus Status.OK
    resp shouldHaveBody "lolcats"
}
Copy code
Body: expected:<"lolcats"> but was:<<empty string>>
d
Currently it's baked into the code with no way to override: https://github.com/http4k/http4k/blob/5eb5a15a335a51da0a0c735cee4517e3ab1e8427/http4k-contract/src/main/kotlin/org/http4k/contract/ContractRoute.kt#L48 It's possible that by putting a check on method into that guard it may work ok. Fork, try it and see if the tests pass..
a
The more I think about it, the more I think what the contract router does is the correct, restful way. I should instead change what I'm trying to do.