Search code examples
scalahttp4shttp4s-circe

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


I am using http4s version 0.18, with Circe and I am getting the error value decode is not a member of org.http4s.AuthedRequest when I converting the json body to a case class within an AuthedService with the following code:

  // 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 =>
          ...
        }
    }

Solution

  • It turns out that, as the documentation specifies, that AuthedService operates on AuthedRequest which is equivalent to (User, Request[F]), so what needs to be done is to call decode on the request part of the AuthedRequest, see:

      // 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 =>
              ...
            }
        }