Is there a way to have one endpoint `/api` that yo...
# http4k
n
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
@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
Excellent. Thank you!
s
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
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
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
Thanks Ivan!