Search code examples
scalahttp4s

Get cookies in middleware in http4s?


I'm trying to write middleware that would extract specific cookie and store information in ContextRequest. Here is my test code:

 def cookie[F[_]: Sync](
               logger: Logger[F]
  ): Kleisli[F, Request[F], ContextRequest[F, Option[Cookie]]] =
    Kleisli { request: Request[F] =>
      for {
      _ <- logger.debug(s"finding cookie")
      _ <- logger.debug(request.cookies.map(_.name).mkString(","))
      } yield ContextRequest(none[Cookie], request)
    }

Then I use it like this:

def httpApp: HttpApp[F] = cookie(logger).mapK(OptionT.liftK).andThen(routesWithCookieContext).orNotFound

The problem is: request doesn't have any cookies even so I see them in the Chrome dev tools and in the request's details in the logs. What I'm doing wrong and how to make it work?


Solution

  • Turned out it was the problem with a cookie content. I was using Circle's .asJson.noSpaces to convert case class into string and write it into cookie's value. But for some reason cookies with json in their value doesn't work.