值解码不是 org.http4s.AuthedRequest 的成员

value decode is not a member of org.http4s.AuthedRequest

我正在使用 http4s 版本 0.18,Circe 在将 json 正文转换为 case class 时出现错误 value decode is not a member of org.http4s.AuthedRequest 15=] 使用以下代码:

  // case class definition
  case class UserUpdate(name: String)

  import org.http4s.AuthedService
  import org.http4s.circe._

  val updateUserService: AuthedService[String, F] =
    AuthedService {
      case req @ PATCH -> Root / "mypath" as _ =>
       req.decode[UserUpdate] { userUpdate =>
          ...
        }
    }

原来,正如documentation所指定的那样,AuthedServiceAuthedRequest进行操作,相当于(User, Request[F]),所以需要做的是在 AuthedRequestrequest 部分调用 decode,参见:

  // case class definition
  case class UserUpdate(name: String)

  import org.http4s.AuthedService
  import org.http4s.circe._

  val updateUserService: AuthedService[String, F] =
    AuthedService {
      case authReq @ PATCH -> Root / "mypath" as _ =>
       authReq.req.decode[UserUpdate] { userUpdate =>
          ...
        }
    }