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

Nezteb

07/31/2019, 5:31 PM
Is there a way to have one endpoint
/api
that you can
POST
to and the handler switches behavior based on the ContentType of the request? I assume I’d do this with lenses, but I can’t see a way to have two different handlers. It looks like I’d have to use one.
s

s4nchez

07/31/2019, 5:33 PM
@Nezteb a handler is a normal function. That means you can have the content negotiation for the endpoint done in a handler which then just call one of the sub-handlers depending on the content type.
n

Nezteb

07/31/2019, 5:35 PM
Excellent. Thank you!
s

s4nchez

07/31/2019, 5:36 PM
In the projects I’ve been involved, however, content negotiation was only used to decide how to extract the domain data from the request, and the rest of the handler logic would be the same. Having completely different behaviours based on content type sounds a bit unusual...
n

Nezteb

07/31/2019, 5:39 PM
In my case it’s just that I have an endpoint that can take CSV or JSON for the same endpoint, so just the parsing step is different, and a little bit of the behavior
👍 1
s

s4nchez

07/31/2019, 5:42 PM
So that’s the normal case then. My suggestion is using a (Request) -> YourDomainType function in the handler to take care of negotiation and delegation to the correct parser (lens or otherwise)
n

Nezteb

07/31/2019, 5:43 PM
Thanks Ivan!
2 Views