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

Andrew O'Hara

10/12/2021, 8:26 PM
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

dave

10/13/2021, 6:31 AM
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

Andrew O'Hara

10/13/2021, 2:35 PM
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.
5 Views