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

Razvan

09/26/2020, 3:39 PM
Hi, getting started with http4k and have some troubles understanding some concepts. I try poking around but got stuck here. I want to get the code from a from posted:
Copy code
val codeField = FormField.required("code")
        val pageForm = Body
                .webForm(Validator.Strict, codeField)
                .toLens()

        val validForm = pageForm(request)
        val code = codeField(validForm)
but when I call the api from a static JS fetch request I get a 500 error:
Copy code
org.http4k.lens.LensFailure: header 'content-type' is not acceptable
for the last line of code. Why does it check the content-type header if it's a Body lens ? And how to get around with it ? the JS fetch request is:
Copy code
fetch{"/check", {
  method: "post",
  body: "code=XXXX"
})
tried with FormData and multipart got the same error.
d

dave

09/26/2020, 3:58 PM
The body lens will expect the content type to be set to "application/x-www-form-urlencoded"
Because you're manually constructing the request as opposed to a standard form submission, which the browser would handle.automatically.
Same with multipart.
You should also use a CatchLensFailure filter to convert the failed extraction into an HTTP 400
r

Razvan

09/26/2020, 4:18 PM
Thank you... I've already tried with specifying the content type, but the site from which I copied called it
application/x-www-form-url-encoded
and that didn't work (lucky me to have found the one site that misspelled the content type). But removing the
-
from
url-encoded
fixed it. I was using CatchAll thought it included LensFailures ones.
d

dave

09/26/2020, 4:21 PM
Catchall just stops an exception from.bubbling out to the server layer. If you want to hide the stacktrace from the client (good practice) this should also be configured as such.
r

Razvan

09/26/2020, 4:56 PM
Yes it's true is not great message the stacktrace as error. (can help in dev). Changed to CatchLensFailure. Again thanks for your help and thanks for the great library... Quite impressed with the quality and the number of things it supports/does. The down side is that as an inexperienced functional programming dev I feel overwhelmed by some concepts and the right way to use them between the doc, site examples and realword one not easy to turn around... It's great to have this place to ask them.
d

dave

09/26/2020, 5:02 PM
If you ever come across something that you don't know how to use, can also recommend checking out the tests in GitHub.
👍 2
160 Views