Is it possible to bind an HttpHandler to not just ...
# http4k
l
Is it possible to bind an HttpHandler to not just a path and HTTP verb but also to a query parameter? I'd like to create a route that only matches if a query parameter is present.
The docs seem to imply this isn't possible but I thought I'd check here to see if I missed anything before I refactor my routes.
Update, it is possible! I found the
org.http4k.routing.queries
function in that documentation page's Code section example. This appears to work:
Copy code
val queryParameterMatchingRoutes = routes(
  "my/path" bind Method.GET to routes(
    queries("myQueryParameter") bind { _ -> Response(Status.OK) }
  )
)
I'm finding the required nesting of the
routes
function to achieve this a little unintuitive. I was expecting something like this to be possible:
Copy code
routes(
  "my/path" bind Method.GET and queries("myQueryParameter") to { _ -> Response(Status.OK) }
)
Anything I'm missing or is the first example above as succinct as it gets?
d
The gist is that any predicate on a request can be converted to a Router and then bound to a verb. The entire "and" dsl is something we did look at briefly but rejected, so it won't happen 🙃
🆗 1
You can and/or Routers together though
l
Got it, I'll stick with the nesting. Thanks @dave!