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

Ray Eldath

01/26/2020, 6:40 AM
Hi everyone 😉 I know that http4k can generate
responseLens
and use
returning
to specify it in contract route. But I want to know is it possible to constraint and auto-marshalling for request? like
Copy code
data class LoginRequest(val email: String, val password: String) {
    init {
        require(EmailValidator.getInstance().isValid(email))
    }
}

val requestLens = Body.auto<LoginRequest>("Login essentials.")
fun handler(login: LoginRequest): HttpHandler =  { req: Request -> ... }

"/login" / Body.of(requestLens) meta {
   ...
} bindContract <http://Method.POST|Method.POST> to ::handler
Many thanks.
d

dave

01/26/2020, 8:15 AM
Absolutely - but the body isn't defined in the path as you've got above (And isn't injected as the path params are). Check out this example from the docs: https://www.http4k.org/guide/modules/contracts/
👍 1
r

Ray Eldath

01/26/2020, 6:43 PM
thanks a lot!!! works like a charm.
👍 1