Hello all, I have a simple ContractRoute based on ...
# http4k
m
Hello all, I have a simple ContractRoute based on the example in the docs (https://www.http4k.org/guide/reference/contracts/). If I send a request to the echo route with the Accept header set to application/xml I get the JSON response back. My expectation was that I would get a 406. I was also expecting to get a 405 if I try to make a request to a defined route using the wrong HTTP method. What do I need to do to implement this behaviour?
d
Content types are just advisory for doc purposes and aren't enforced inside the contracts. You can write a custom filter to enforce it easily. For the 405, it's also not currently a feature of the contract routing.
m
Thanks @dave. Is there an example of this?
d
something like this works
Copy code
fun Require(contentType: ContentType) = Filter { next ->
    {
        when (Header.CONTENT_TYPE(it)) {
            contentType -> next(it)
            else -> Response(NOT_ACCEPTABLE)
        }
    }
}
(or whatever status code you want)
👍 1