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

Matt

08/18/2021, 11:21 AM
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

dave

08/18/2021, 12:59 PM
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

Matt

08/18/2021, 1:33 PM
Thanks @dave. Is there an example of this?
d

dave

08/20/2021, 6:27 PM
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
9 Views