我有一个通过网络发送的Post请求,以获取与我使用Http4s的用户相关的数据。
在编写HttpRoutes时,我使用它来处理POST的情况,如下所示:
case req @ POST -> Root/ "posts" { "name": username, "friends": friends} =>name和friends是作为参数在请求体中传递的属性。
然而,我似乎可以识别出'=>' expected but '{' found存在一些语法错误。
发布于 2021-01-19 20:38:01
这是一个不正确的Scala语法。这里有一个来自官方http4s docs的例子
case req @ POST -> Root / "hello" / id =>
for {
// Decode a User request
user <- req.as[User]
// Encode a hello response
resp <- Ok(Hello(user.name).asJson)
} yield (resp)您正在通过"/hello“路由访问API。然后将请求解码(解组)为User实例。例如,您可以使用Circe JSON库来解码来自请求的内容:
import io.circe.generic.auto._
import io.circe.syntax._
import org.http4s._
import org.http4s.circe._id是一个path变量。在这里,您也可以看看如何使用查询参数:https://http4s.org/v0.21/dsl。
Circe编码器用于将User实例转换为响应的JSON内容。
Ok(Hello(user.name).asJson)https://stackoverflow.com/questions/65790554
复制相似问题