Http4s Tips and Tricks

January 1, 2021 • ☕️ 1 min read

Functional ScalaScalaNotesHttp4s

Get the text body of a response with EntityDecoder

If you’ve got a request like this:

val postRequest: Task[Request[Task]] = POST(
                                     payload,
                                     uri
                                   )

and you just want the body as text (e.g. for debug), use:

postRequest.map { req =>
  client.run(req).use {
    case Status.Successful(resp) =>
      EntityDecoder.decodeText(resp).flatMap { respText =>
        logger.info(s"The body of the response as text is: " + respText)
      }
  }
}