简体   繁体   English

如何使用 http4s 处理基本表单提交?

[英]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.我不敢相信这不在http4s 文档中,而且我能够在线挖掘的示例代码(经过足够长的时间发现UrlForm类之后)对我不起作用。

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.提交关联表单时,会IllegalStateException ps.values is an empty map ( Map() ). ps.values是一个空映射( 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.我可以看到(使用printlnContent-Typeapplication/x-www-form-urlencoded ,正如预期的那样,我可以从浏览器的 Network 选项卡中看到请求“参数”(编码的表单值)正在正确发送.

The problem is that I had a filter ( javax.servlet.Filter ) in place that was calling getParameterMap on the HttpServletRequest .问题是我有一个过滤器( javax.servlet.Filter )在HttpServletRequest上调用getParameterMap This was draining the InputStream for the request, and it was happening before the request got passed off to the servlet ( BlockingHttp4sServlet ) instance.这正在耗尽请求的InputStream ,并且它发生在请求传递到 servlet ( BlockingHttp4sServlet ) 实例之前。

It seems to me the BlockingHttp4sServlet should raise an IllegalStateException (or something more descriptive) when it receives an InputStream with isFinished returning true.在我看来,当BlockingHttp4sServlet收到一个isFinished返回 true 的InputStream时,它应该引发IllegalStateException (或更具描述性的isFinished (I've filed an issue with the http4s project on Github.) (我已经在 Github 上向 http4s 项目提交了一个问题。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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