简体   繁体   中英

How to handle a basic form submission with http4s?

I can't believe this isn't in the http4s documentation , and the example code I was able to dig up online (after poking around long enough to discover the UrlForm class) is not working for me.

The relevant bit of code looks like this:

case req @ POST -> Root / "compose" =>
  req.decode[UrlForm] { ps =>
    println("ps.values: " + ps.values)
    val content = ps.getFirstOrElse("content",
      throw new IllegalStateException("No content given!"))
    // Do something with `content`...
  }

When submitting the associated form, the IllegalStateException is thrown. ps.values is an empty map ( Map() ).

I can see (using println ) that the Content-Type is application/x-www-form-urlencoded , as expected, and I can see from my browser's Network tab that request "paramaters" (the encoded form values) are being sent properly.

The problem is that I had a filter ( javax.servlet.Filter ) in place that was calling getParameterMap on the HttpServletRequest . This was draining the InputStream for the request, and it was happening before the request got passed off to the servlet ( BlockingHttp4sServlet ) instance.

It seems to me the BlockingHttp4sServlet should raise an IllegalStateException (or something more descriptive) when it receives an InputStream with isFinished returning true. (I've filed an issue with the http4s project on Github.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM